Home  >  Article  >  Backend Development  >  PHP global keyword, phpglobal keyword_PHP tutorial

PHP global keyword, phpglobal keyword_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:07:45959browse

PHP global keyword, phpglobal keyword

global keyword is used to access global variables inside a function.

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

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

myTest();
echo $x; //15

PHP stores all global variables in an array named $GLOBALS[index]. Index saves the name of the variable. This array can be accessed inside the function, or global variables can be updated directly;

The above example can be rewritten as follows:

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

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

myTest();
echo $x; //15

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1059468.htmlTechArticlePHP global keyword, phpglobal keyword The global keyword is used to access global variables inside a function. ?php$x = 5;$y = 10;function myTest(){ global $x,$y; $x = $x $y;}myTest();echo $x...
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