Ok I have another question regarding globals.
If I have this for example
<?php
global $life, $num;
$life = 42;
$num = 32;
function meaningOfLife()
{
echo "The meaning of life is " . $life . ".</br/>";
echo "The second number is " . $num . ".";
}
meaningOfLife();
?>
With this code, the script pops two errors.
Warning: Undefined variable $life in H:\XAMPP\htdocs\Runes\Vars.php on line 11
The meaning of life is .
**Warning**: Undefined variable $num in **H:\XAMPP\htdocs\Runes\Vars.php** on line **12**
The second number is .
Why are the vars considered undefined in this case. It seems to me if I declare both of them as globals anywhere inside the script they should be available to any function, no matter how many functions I use.
If I add a second function like this
<?php
function meaningOfLife()
{
global $life;
global $num;
$life = 42;
$num = 32;
echo "The meaning of life is " . $life . “.</br/>”;
echo "The second number is " . $num . “.”;
}
meaningOfLife();
function meaningOfLife2()
{
echo "The second meaning of life is " . $life . ".</br/>";
echo "The second second number is " . $num . ".";
}
meaningOfLife2();
?>
Then I get the error message about undeclared vars in the second function. To me, this just seems like a waste to declare vars as global unless I am just missing something.
I have tried reading up on this but nothing seems to address this question.
Thanks again!
Jim