Introduction to the usage of php global keyword

怪我咯
Release: 2023-03-10 20:30:01
Original
1340 people have browsed it

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
Copy after login

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
Copy after login

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!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template