PHP global 关键字,phpglobal关键字_PHP教程

WBOY
Release: 2016-07-12 09:07:45
Original
898 people have browsed it

PHP global 关键字,phpglobal关键字

global关键字用于在函数内部访问全局变量。

<?php
$x = 5;
$y = 10;

function myTest(){  
    global $x,$y;
    $x = $x+$y;
}

myTest();
echo $x; //15
Copy after login

php将所有全局变量存储在一个名为$GLOBALS[index]的数组里,index保存变量的名称,这个数组可以在函数内部访问,也可以直接更新全局变量;

上面的实例可以改写成下面这个样:

<?php
$x = 5;
$y=10;

function myTest(){
    $GLOBALS['x'] = $GLOBALS['x'] + $GLOBALS['y'];
}

myTest();
echo $x; //15
Copy after login

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1059468.htmlTechArticlePHP global 关键字,phpglobal关键字 global关键字用于在函数内部访问全局变量。 ?php$x = 5;$y = 10;function myTest(){ global $x,$y; $x = $x+$y;}myTest();echo $x...
Related labels:
php
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 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!