Home  >  Article  >  Backend Development  >  Demo code for using global variable global in php_PHP tutorial

Demo code for using global variable global in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:30:06759browse

Let me explain to those who are new to the industry the use of global variables, "global variables". The two words "global" in this noun already tell us that this variable can be used everywhere. Let's look at an example first:

Copy code The code is as follows:

$a = 1;
$b = 2;
function Sum()
{
global $a, $b; //Declared as a global variable inside
$b = $a + $b;
}
Sum();
echo $b;
?>

Result: 3
If there is no global variable global, the values ​​​​of $a and $b cannot be obtained in the method, so in the method If you want to use an external variable, you need to declare the variable as a global variable first, so that it can be used. It is very convenient.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323312.htmlTechArticleLet me explain the use of global variables to those who are new to the industry, "global variables", in this noun The word global already tells us that this variable can be used everywhere, let’s look first...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn