I'm trying to use global variables in my php script, and I found the usage of global variable on php.net. But it doesn't work on my local server. Is there any configuration I have missed out?
There is an example found on that page:
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
The above script will output 3.
However my output is 2!
Another example:
<?php
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
?>
I get error:
Undefined index: a
So what is the deal with that? Why doesn't my code work as expected?
By they way, I'm using Laravel.