Hi All!
While exploring the $GLOBALS array, I wrote some code that clearly (IMHO) shows how the $GLOBALS array works.
INSTRUCTIONS:
Copy the code below into an editor and save it as plain text with the name, 'listglobals.php'.
Temporarily login as root, using the 'su' command at the bash shell prompt. Then copy the file 'listglobals.php' to your web server. Be sure to exit 'su' as soon as copying is complete. For example, using the Apache 2.0 web server:
su (enter the root password when prompted)
cp -v listglobals.php /srv/www/htdocs/
exit
Run the code by opening 'listglobals.php' in your web browser. Look at the output on the webpage, then look carefully at the code and you should be able to figure out what's going on.
NOTE: I first tried to loop thru the array listing the contents, but the While loop never ended. At first, I was puzzled by the results, until I rewrote the program using the foreach code below. Hint: The array was constantly growing, therefore the While loop would run on forever!!
SAMPLE CODE:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
$j = count($GLOBALS);
echo "The GLOBALS array has " . $j . " elements:<br><br>";
$arr = $GLOBALS;
$i = 0;
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br>\n";
$i++;
}
echo "<br>But wait... the program looped thru " . $i . " elements.<br>";
echo "Do you see what just happened?<br>";
echo "The GLOBALS array is dynamic, which means it's constantly changing--even while your code is running!!<br>";
?>