Home >Backend Development >PHP Tutorial >PHP global keyword

PHP global keyword

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:14:121070browse

The

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

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

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

myTest();
echo $t; //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 directly updated global variables;

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
<span> </span>

The above introduces the PHP global keyword, including global variables. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:nginScript笔记Next article:thinkphp学习笔记2