In PHP, variables that are defined in a function, including parameters, cannot access the external variables of the function, and, by default, variables that are defined outside a function cannot be accessed by a variable.
Below is an example of the code below:
<?php
$a = 1;
$b = 2;
function Sum ()
{
Global $a, $b;
$b = $a + $b;
}
Sum ();
Echo $b;
?>
This returns the value of $b to 3, and in PHP, Global is an all-in-one variable, then we now look at the PHP variable reference instance, the code is as follows:
<?php
Function Str_unite (& $string)
{
$string. = ' also like blue. ';
}
$str = ' like red, ';
Str_unite ($STR);
Echo $str; Output: ' Like red, also like blue. '
?>
Above is the function of the scope of the global variables and functions of reference, the following is a function of the local variables, the code is as follows:
<?php
$a = 1;
$b = 2;
function Sum ($a, $b)
{//Open source code phpfensi.com
$b = $a + $b;
echo $b;//3
}
Sum ();//
echo $b;//2
?>