sshelby Posted June 28, 2007 Share Posted June 28, 2007 My host recently upgraded to PHP Version 4.4.7. After the upgrade any foreach statement using the following format suddenly stopped working. foreach (array_expression as $value) I diagnosed the problem by adding a print statement on the value of $value and it turns out that instead of the actual value of the element it returns an array. I replaced them with the following format and all is well but would like to understand what happened because the PHP manual lists both as valid options. Does anyone have any ideas? I couldn't find any reference to a bug or anything. foreach (array_expression as $key => $value) Thanks! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 28, 2007 Share Posted June 28, 2007 Thats strange, the first way works fine in PHP5. foreach (array_expression as $value) Maybe they took it out when they release PHP4 for some reason, then added it back in with PHP5. Hmm...I can't give you any specifics, sorry. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 28, 2007 Share Posted June 28, 2007 $array = array('a', 'b', 'c'); foreach($array as $v) { echo $v; } that would echo 'abc'; Quote Link to comment Share on other sites More sharing options...
per1os Posted June 28, 2007 Share Posted June 28, 2007 Show us what your array looks like. and what you mean by "array_expression" Quote Link to comment Share on other sites More sharing options...
sshelby Posted June 28, 2007 Author Share Posted June 28, 2007 Here is an example: $reserved_key[] = 'a; $reserved_key[] = 'b'; foreach ($reserved_key as $value) echo $value; array is printed. Quote Link to comment Share on other sites More sharing options...
per1os Posted June 28, 2007 Share Posted June 28, 2007 Sounds like your php.ini is tweaked funny. I just ran that (with the syntax correction) and it printed "ab". Quote Link to comment Share on other sites More sharing options...
lifeson2112 Posted June 28, 2007 Share Posted June 28, 2007 Maybe the array you are accessing contains an array in each element (two-dimensional array)? your example above looks oddly declared. Shouldn't you have something like $reserved_key[0] = 'a'; or something. Or does it automatically assign the value to whatever the 0 points to? either way, maybe you should try assigning values in your array differently. Quote Link to comment Share on other sites More sharing options...
sshelby Posted June 28, 2007 Author Share Posted June 28, 2007 Maybe the array you are accessing contains an array in each element (two-dimensional array)? your example above looks oddly declared. Shouldn't you have something like $reserved_key[0] = 'a'; or something. Or does it automatically assign the value to whatever the 0 points to? either way, maybe you should try assigning values in your array differently. It assigns values to elements incrementally, so that shouldn't be a problem. The long method of foreach works so I'm not sure what the deal is. frost110, does your host use version 4.4.7? Quote Link to comment Share on other sites More sharing options...
per1os Posted June 28, 2007 Share Posted June 28, 2007 Apache/1.3.37 (Unix) PHP/4.4.4 Quote Link to comment 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.