$_POST
is a superglobal containing data passed to the script in the HTTP body (e.g. from a <form>
with method="post"
) - you can't really "write" to $_POST
at runtime in the way you're trying to.
A neater way to write what you're attempting, assuming I understand it correctly (and one that might actually work) would be:
// variable definition
$iPainScore = 0;
// evaluation
if(!empty($_POST)) {
// increment $iPainScore by the value of $_POST['pain']
// unless $_POST['pain'] == 1, in which case increment $iPainScore by 3
$iPainScore += $_POST['pain'] == 1 ? 3 : (int) $_POST['pain'];
}
// output
echo "The score is {$iPainScore}";
EDIT
... actually, since $_POST['pain']
can only ever be 0 or 1 you don't really need to add it to your pain score at all; you're using $_POST['pain']
as a boolean it seems, actually incrementing your pain score by 3 if $_POST['pain']
is 'true' ( for (int) 1 == (bool) true ).
You could therefore reduce the entire thing (assuming you're starting here with a pain score of 0 rather than incrementing an existing pain score) to:
$iPainScore = !empty($_POST['pain']) ? 3 : 0;
echo "The score is{$iPainScore}"'