global keyword and $GLOBALS usage in php

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

First let’s look at a piece of code

<?php
$a  =  1 ;  /* global scope */

function  Test ()
{
    echo  $a ;  /* reference to local scope variable */
}

Test ();
?>
Copy after login

This script will not have any output, because the echo statement references a local version of the variable $a, and within this scope, it has not been assigned a value. You may notice that PHP's global variables are a little different from C language. In C language, global variables automatically take effect in functions unless overridden by local variables. This may cause some problems, someone may accidentally change a global variable. Global variables in php must be declared global when used in functions.

global keyword

First, an example of using global, the code is as follows:

<?php
$a  =  1 ;
$b  =  2 ;

function  Sum ()
{
    global  $a ,  $b ;

     $b  =  $a  +  $b ;
}

Sum ();
echo  $b ;
?>
Copy after login

The output of the above script will be "3". After global variables $a and $b are declared in a function, all references to either variable will point to its global version. PHP has no limit on the maximum number of global variables that a function can declare.

The second way to access variables in the global scope is to use a special PHP custom $GLOBALS array. The previous example can be written as:

Example #2 Use $GLOBALS instead of global, the code is as follows:

<?php
$a  =  1 ;
$b  =  2 ;

function  Sum ()
{
     $GLOBALS [ &#39;b&#39; ] =  $GLOBALS [ &#39;a&#39; ] +  $GLOBALS [ &#39;b&#39; ];
}

Sum ();
echo  $b ;
?>
Copy after login

$GLOBALS is an associative array, each variable is an element, and the key name corresponds to Variable name, the value corresponds to the content of the variable. The reason $GLOBALS exists in the global scope is because $GLOBALS is a superglobal variable. The following example shows the use of superglobal variables:

Example #3 Example demonstrating superglobal variables and scope

<?php
function  test_global ()
{
     // 大多数的预定义变量并不 "super",它们需要用 &#39;global&#39; 关键字来使它们在函数的本地区域中有效。
     global  $HTTP_POST_VARS ;
    echo  $HTTP_POST_VARS [ &#39;name&#39; ];
     // Superglobals 在任何范围内都有效,它们并不需要 &#39;global&#39; 声明。Superglobals 是在 PHP 4.1.0 引入的。
     echo  $_POST [ &#39;name&#39; ];
}
?>
Copy after login

The above is the detailed content of global keyword and $GLOBALS usage in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!