Northern Flame Posted December 16, 2007 Share Posted December 16, 2007 I have a script that I am working on and I had a problem the the foreach() command. I was having a hard time figuring out why it wasnt working and I was messing around with some stuff and I figured out that I cant use the array that I established in my function. Take the following sample script as an example: <?php function testIng(){ $char['test'] = 'test'; $char['testing'] = 'testing'; global $char; } testIng(); foreach($char as $key => $val){ echo $key . ' - ' . $val . "<br>\n"; } ?> with that I get this error Warning: Invalid argument supplied for foreach() in /path/to/my/website.com/script.php on line 8 can anyone tell my why I cant use the array established in the function testIng()? Quote Link to comment Share on other sites More sharing options...
Trium918 Posted December 16, 2007 Share Posted December 16, 2007 <?php /* Try this! Result test - test testing - testing */ function testIng(){ $char['test'] = 'test'; $char['testing'] = 'testing'; return $char; } $char = testIng(); foreach($char as $key => $val){ echo $key . ' - ' . $val . "<br>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted December 16, 2007 Author Share Posted December 16, 2007 Thanks a lot! WORKS PERFECTLY! 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.