"By placing the global statement in front of the $life variable when it is declared in the meaningOfLife() function...it now refers to the $life variable declared outside the function..." (139) In this example, the global statement inside the function alerts the PHP interpreter to treat this varaible as global. "You will need to use the global statement within every function that needs to access a particular named global variable. Be careful, though: If you manipulate the contents of the variable within the function, the value of the variable will be changed for the script as a whole." (140) More than one variable at a time can be declared with the global statement, by separating them with commas. Here's one more reminder from the text: "Usually, an argument is a copy of whatever value is passed by the calling code; changing it in a function has no effect beyond the function block. Changing a global variable within a function, on the other hand, changes the original and not a copy. Use the global statement carefully." (140)
<?php
$life=42;
function meaningOfLife() {
global $life;
echo "The meaning of life is ".$life;
}
meaningOfLife();
?>