PHP global keyword, phpglobal keyword_PHP tutorial

WBOY
Release: 2016-07-12 09:07:45
Original
964 people have browsed it

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

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