Home > php教程 > php手册 > php中全局变量global的使用演示代码

php中全局变量global的使用演示代码

WBOY
Release: 2016-06-13 12:09:58
Original
1063 people have browsed it

我来给处入行的人讲解一下全局变量global的使用,”全局变量“,这个名词中的全局两个字已经告诉我们这个变量在各个地方都能用,先看一个实例:

复制代码 代码如下:


$a = 1;
$b = 2;
function Sum()
{
global $a, $b; //在里面声明为全局变量
$b = $a + $b;
}
Sum();
echo $b;
?>


结果: 3
如果没有全局变量global在方法内是不能获得$a,$b值的,所以在方法里面想用外面的变量就需要先声明这个变量为全局变量,这样就可以使用了,很方便吧。
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template