PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
On a server running PHP 4.4, the code below prints
1123
But if I remove the global declaration it prints
1123
2123
3123
Does anyone know if this is a bug, or if it was a feature changed in later version of PHP (A server running PHP 5.2.11 gives the latter result set with or without the global declaration.
http://us3.php.net/manual/en/control-st ... oreach.php
Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
For example, is the only safe coding to make a copy of the array and do the inner foreach against that copy?
And: If I were coding only for 5.2 and above, would I need to take that precaution? That is, is the current behavior now considered stable and correct?
jrisken wrote:That's helpful. But can you say more?
For example, is the only safe coding to make a copy of the array and do the inner foreach against that copy?
And: If I were coding only for 5.2 and above, would I need to take that precaution? That is, is the current behavior now considered stable and correct?
I would say to make a copy of it. The behavior changed between versions and may change again, and all you will get from the PHP devs is: foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it. So, do not rely on the behavior being consistent.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.