If you do not take all this into the function, then the result works. And if this is how to perform everything in the function, then PHP swears:

"Invalid argument supplied for foreach ()"

I concluded that I am incorrectly passing an array as an argument for my user-defined function resultBuyList() :

 $buyList = array( array('name' => 'Телевизор', 'price' => '400', 'quantity' => 1), array('name' => 'Телефон', 'price' => '300', 'quantity' => 3), array('name' => 'Кросовки', 'price' => '150', 'quantity' => 2), ); function resultBuyList($buyList) { foreach ($buyList as $key => $value) { $credits = $value['quantity'] * $value['price']; $creditCount += $credits; $productCount += $value['quantity']; } return $result = "Вы купили $productCount единиц товара, общая сумма к оплате: $creditCount"; } resultBuyList($buyList); 
  • Maybe you just sometimes pass an array containing nothing in it? foreach in such a message swears on it usually. А если вот так в функции все выполнять, тогда PHP ругается - so how is it? It is better to completely write as you try to write ... exactly as when swearing - Aleksey Shimansky
  • This is the code that I indicated in the question I want to run. When I run, I get this error with an Invalid argument. I want to know what I'm doing wrong and how to make the code work. - Yevgeny Fichak
  • It is the code that is in question and exactly in the form that it is - a worker .... except that the initial values ​​for creditCount and productCount are not announced - Alexey Shimansky
  • I really apologize for arguing, but I assure you that this code (exactly as it is) gives the error "Invalid argument supplied for foreach ()". True, try to start - see this. - Yevgeny Fychak
  • Duc ....... ideone.com/ZtJ6kP =) - Alexey Shimansky

1 answer 1

 function resultBuyList($buyList) { if (!is_array($buyList) { return "Вы ничего не купили, но вам всё равно придется заплатить 1 000 000 рублей"; } ... } 

And there will be no such mistakes.