Home > Backend Development > PHP Tutorial > PHP global keyword

PHP global keyword

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:14:12
Original
1057 people have browsed it

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

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

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

Related labels:
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