Home >Backend Development >PHP Tutorial >Introduction to the usage of php global keyword

Introduction to the usage of php global keyword

怪我咯
怪我咯Original
2017-06-19 16:07:251374browse

The

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

Sample code

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

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

myTest();
echo $x; //15

php stores all global variables in a array named $GLOBALS[index]. index saves the name of the variable. This array can be For internal access within the function, you can also directlyupdateglobal variables;

The above example can be rewritten as follows:

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

function myTest(){
    $GLOBALS[&#39;x&#39;] = $GLOBALS[&#39;x&#39;] + $GLOBALS[&#39;y&#39;];
}

myTest();
echo $x; //15

The above is the detailed content of Introduction to the usage of php global keyword. For more information, please follow other related articles on the PHP Chinese website!

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