Jump to content

foreach Using GLOBALS Variable


digiman999

Recommended Posts

Hi peeps,
 
      I have this code here in a function.php file.
 
$row = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM releases"));
$GLOBALS ['releaseids']=$row['id']; global $releaseids;
 
I then try & use this within a function
 
function postcontainer(){
global $releaseids;
foreach ($releaseids as $releaseid){
echo "this is the id";}
}}
 
 
Every time however, I keep getting 
 
 Invalid argument supplied for foreach() in...
 
This still applies too if it isn't put inside or outside of a function.
 
Very grateful
digiman 
Link to comment
https://forums.phpfreaks.com/topic/278438-foreach-using-globals-variable/
Share on other sites

Okay, don't even bother trying to do it that way.

 

Pass the values to the function. Plus, a function shouldn't really echo anything, it should just return the data. You don't need a function to do what you're trying to do, just the foreach() wherever your values are already defined.

 

Stop using global completely. Forget that word exists.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.