Home  >  Article  >  php教程  >  php中局部变量和全局变量

php中局部变量和全局变量

WBOY
WBOYOriginal
2016-05-25 16:46:48935browse

在php中如何来理解作用域呢,下面我们来介绍一下关于局部变量:在函数内部声明的变量.全局变量: 在函数外部声明的变量,作详细的说明

局部变量转换成全局变量

实例代码如下:

<?php 
    $a = 5; 
    function funcChangeValue() 
    { 
        global $a; 
        $a = 10; 
    } 
     
    funcChangeValue(); 
     
    echo $a; 
?>

output

10

超级全局变量$GLOBALR的使用

实例代码如下:

<?php 
    $GLOBALS[&#39;a&#39;] = 5; 
    function funcChangeValue() 
    { 
        $GLOBALS[&#39;a&#39;] = 10; 
    } 
     
    funcChangeValue(); 
     
    echo $GLOBALS[&#39;a&#39;]; 
?>

Output

10


文章链接:

随便收藏,请保留本文地址!

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