A
A
Alexander Andropov2021-08-06 22:04:23
PHP
Alexander Andropov, 2021-08-06 22:04:23

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;

As a result, I get the amount, but in addition, in addition, the error Warning: Undefined variable . How to remove it? I declare a variable TO.
If you declare a variable like this or like this Then it will issue a Warning: A non-numeric value encountered in The data in the $zapros['bids'][$i][1] array is like this: 403.40000000, 4800.00000000, 24085.90000000, etc. I just need to add them all together and so that there are no errors :) $bidsall = 0;global $bidsall;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene Ordinary, 2021-08-06
@LongOf

addition of a number += with a string (dot operation . )
$bidsall += $zapros['bids'][$i][1] . "<br>"

T
ThunderCat, 2021-08-06
@ThunderCat

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 in
Which 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();
And why do you have br there? Do you fold or join?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question