The use of the global variable global in php
- 2020-05-07 19:18:01
- OfStack
Let me show you how to use the global variable global, "global variable." the word "global" in this noun already tells us that this variable can be used everywhere.
Results: 3
If there is no global variable global, the value of $a and $b cannot be obtained in the method, so if you want to use the outside variable in the method, you need to declare this variable as a global variable first, so you can use it.
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b; // Declare it as a global variable
$b = $a + $b;
}
Sum();
echo $b;
?>
Results: 3
If there is no global variable global, the value of $a and $b cannot be obtained in the method, so if you want to use the outside variable in the method, you need to declare this variable as a global variable first, so you can use it.