Answer the question
In order to leave comments, you need to log in
Why am I getting a Warning: Undefined variable error when summing in a loop?
Good day. There is a task to summarize all values from $zapros['bids'] and display the result, the sum of all values. I do it like this:
$bidsall;
for ($i = 0; $i < count($zapros['bids']); $i++) {
$bidsall += $zapros['bids'][$i][1] . "<br>";
}
echo $bidsall;
$bidsall = 0;
global $bidsall;
Answer the question
In order to leave comments, you need to log in
addition of a number += with a string (dot operation . )
$bidsall += $zapros['bids'][$i][1] . "<br>"
Firstly - not an error, but a warning, and secondly - read in full, and not just a piece of the warning.
thirdly, the fact that you wrote it is not an announcement, that is, $bidsall is still null for you, but according to your mind it should be 0.
If you declare a variable like this $bidsall = 0;Then everything will be ok
Then it will issue Warning: A non-numeric value encountered inWhich also needs to be read in full, and most likely refers to the values that you add, and not to this variable. Cast via floatval();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question