Home  >  Article  >  Backend Development  >  Local variables and global variables in php_PHP tutorial

Local variables and global variables in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:071121browse

How to understand scope in PHP? Let’s introduce local variables: variables declared inside functions. Global variables: Variables declared outside the function should be explained in detail

7.2.8 Convert local variables into global variables

The code is as follows
 代码如下 复制代码

$a = 5;

function funcChangeValue()

{

global $a;

$a = 10;

}

funcChangeValue();

echo $a;

?>
 

output

10

Copy code

$a = 5;

代码如下 复制代码

$GLOBALS['a'] = 5;

function funcChangeValue()

{

$GLOBALS['a'] = 10;

}

funcChangeValue();

echo $GLOBALS['a'];

?>
 

Output

10

function funcChangeValue() { global $a;

$a = 10;} funcChangeValue(); echo $a; ?>
output
10
7.2.9 Use of super global variable $GLOBALR
The code is as follows Copy code
<🎜>$GLOBALS['a'] = 5;<🎜> <🎜>function funcChangeValue()<🎜> <🎜>{<🎜> <🎜> $GLOBALS['a'] = 10;<🎜> <🎜>}<🎜> <🎜> <🎜> <🎜>funcChangeValue();<🎜> <🎜> <🎜> <🎜>echo $GLOBALS['a'];<🎜> <🎜>?>
Output 10 http://www.bkjia.com/PHPjc/629235.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629235.htmlTechArticleHow to understand scope in PHP? Let’s introduce local variables: declared inside a function variable. Global variables: variables declared outside the function, detailed description...
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