如何使用输入表单更改PHP变量?

I have sets of arrays that each have a value attributed to them. This value goes through a function to find a percentage then it is displayed on the page. Here's a snippet of that code:

<?php
$balls = array(
    'blue' => 4,
    'red' => 13,
    'green' => 5
);

function percent($array, $element)
{
    $total = array_sum($array);
    $elementValue = $array[$element];
    return ($elementValue / $total) * 100;
}
?>

<html>
<body>
Blue balls per set:<?php echo $balls["blue"]; ?>.
Percentage of blue balls:<?php echo percent($balls, 'blue'); ?>%.
</body>
</html>

How do I add an input form that could change this equation around. Specifically, I want to be able to add a variable ($sets) that would multiply the values of the array items before they are summed up.

The closest thing I can think of is something like this:

$sets = value from input form, if no value entered, default as 1

$balls = array(
    'blue' => 4 * $sets,
    'red' => 13 * $sets,
    'green' => 5 * $sets
);

The rest of the equation would work as usual, but be updated with the new value from $sets as submitted in the input form. Also, would such a thing update the numbers displayed on the webpage instantly?

If $sets is the only thing you're interested in changing, you can easily set up a small HTML form (assuming index.php is the name of your PHP file):

<form action="index.php" method="post">
 Number of sets: <input type="text" name="sets" />
 <br /><input type="submit" value="Get sum!" />
</form>

In your PHP code, you can then do the following to get the value when someone submits the HTML form:

$sets = 1; // default value

// Make sure a value was sent
if(isset($_POST['sets'])) {
    // Check that sets is numeric, and above 0
    if(is_numeric($_POST['sets']) && $_POST['sets'] > 0) {
        $sets = $_POST['sets'];
    }
}

// Do computation with $sets...

Yes, if you send a value from the HTML form to your PHP page, the values on your page will be according to what you sent in your form immediately.

It looks like you're starting with PHP (or programming in general) – best of luck! One important rule to remember is to validate user input, like we did here: we've checked that we were sent a number (as that's what we require) and that the value was bigger than 0.

We've only added the value of $_POST['sets'] to our variable once we were sure it met our criteria. You never know what people might try to send you and you want to be prepared for any case.

Setting $sets to 1 in the beginning means we're starting with a default value and we can just worry about setting $sets to something else when we want to. Consider this in contrast:

if(isset($_POST['sets'])) {
    if(is_numeric($_POST['sets']) && $_POST['sets'] > 0) {
        $sets = $_POST['sets'];
    }
    else {
        $sets = 1;
    }
}
else {
    $sets = 1;
}

This is less nice because it is less obvious that $sets will, positively, have a valid value in any given case. It also means that you need to change 1 to another number if you decide to change the default value.

This is rather trivial in this example since the code is small, but I'm sure you can imagine the benefit once you write bigger code with more complex checks.