Home > php教程 > php手册 > body text

php中局部变量和全局变量

WBOY
Release: 2016-05-25 16:46:48
Original
934 people have browsed it

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

局部变量转换成全局变量

实例代码如下:

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

output

10

超级全局变量$GLOBALR的使用

实例代码如下:

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

Output

10


文章链接:

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

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!