david-king Posted May 9, 2008 Share Posted May 9, 2008 I'm confused with some odd behaviour with foreach, if have the following code: $arr = array (); foreach ($arr['doesnotexist'] as $v) { // ... } print_r ($arr); // Outputs: Array ( [doesnotexist] => ) So it creates $arr['doesnotexist'] as it was not set, however the following does not create anything: foreach ($doesnotexist as $v) { // ... } print_r ($doesnotexist); // Outputs nothing Does anyone know why this happens, and if there's an alternative looping method?? Quote Link to comment https://forums.phpfreaks.com/topic/104914-why-does-foreach-create-a-new-variable-if-there-is-none-set/ Share on other sites More sharing options...
robos99 Posted May 9, 2008 Share Posted May 9, 2008 My guess would be, in your first example you're not referencing just the array, but the array slice 'doesnotexist'. This doesn't exist, but since you're passing it to foreach in that manner, the fuction might be assuming that 'doesnotexist' is also an array. And since it doesn't exist, PHP is initializing it when you run foreach. At least, that's my theory. Quote Link to comment https://forums.phpfreaks.com/topic/104914-why-does-foreach-create-a-new-variable-if-there-is-none-set/#findComment-537001 Share on other sites More sharing options...
wildteen88 Posted May 9, 2008 Share Posted May 9, 2008 robos you are incorrect. foreach loops do not initiate variables/array keys if they don't exist. If I run the following code: <?php $arr = array(); foreach($arr['test'] as $v) { echo $v; } echo '<pre>' . print_r($arr, true) . '</pre>'; ?> I get the following output: Notice: Undefined index: test in C:\Server\www\test.php on line 3 Warning: Invalid argument supplied for foreach() in C:\Server\www\test.php on line 3 Array ( ) Quote Link to comment https://forums.phpfreaks.com/topic/104914-why-does-foreach-create-a-new-variable-if-there-is-none-set/#findComment-537008 Share on other sites More sharing options...
david-king Posted May 9, 2008 Author Share Posted May 9, 2008 @robos Yeah, I imagine it's something along those lines, however the datatype is actually set to "NULL" which I'm finding a little hard to understand $arr = array (); foreach ($arr['doesnotexist'] as $v) { // ... } print_r ($arr); // Outputs: Array ( [doesnotexist] => ) echo gettype ($arr['doesnotexist']); // Outputs: NULL Maybe it's an internal problem / bug... Hmm @wildteen What version of PHP are you running to get those settings? I'm on PHP 4.3.9, safe mode is on Quote Link to comment https://forums.phpfreaks.com/topic/104914-why-does-foreach-create-a-new-variable-if-there-is-none-set/#findComment-537013 Share on other sites More sharing options...
BlueSkyIS Posted May 9, 2008 Share Posted May 9, 2008 i get Warning: Invalid argument supplied for foreach() in /Users/lesbrown/Sites/site1/test.php on line 3 Quote Link to comment https://forums.phpfreaks.com/topic/104914-why-does-foreach-create-a-new-variable-if-there-is-none-set/#findComment-537038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.