Home > Backend Development > PHP Tutorial > global keyword and $GLOBALS usage in PHP

global keyword and $GLOBALS usage in PHP

WBOY
Release: 2016-08-08 09:19:33
Original
1155 people have browsed it

<span><span><?php<br>$a </span><span>= </span><span>1</span><span>; </span><span>/* <span>global</span> scope */<br></span><span>function </span><span>Test</span> <span>()<br>{<br> echo </span><span>$ this script will not have any output because The echo statement refers to a local version of the variable </span><span>$a</span><span>, and within this scope, it is not 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 as <br>global</span>. <span><br></span><span>global</span> Keyword <span><br>First, an example using </span><span>global</span></span>:

Example #1 Using

global

$a

= 1

;$b

=

2;

function

Sum<span>()<span>{<br> </span>global<span> </span><span>$a</span><span>, <br></span>$b<span></span>;<span> + </span><span>$b</span><span>;<br>}</span> <span></span>Sum<span><br>();<br>echo <span></span>$b</span><span>;</span><span></span>?> After global variables <span></span>$a<span><br> and </span><span>$b</span><span> 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. </span><span>The second way to access variables in the global scope is to use special PHP customization </span><span>$GLOBALS </span><span>Array. The previous example could be written as: </span><span><br>Example #2 using <br></span>$GLOBALS <span></span>replacement<span>global<br></span><span></span><span><br></span><?php<span>$a </span></span>=

1

;

$b = 2 ;

function

Sum( ){

$GLOBALS['b'] =

$GLOBALS

[<span><span>'a'<br></span>] + <span></span>$GLOBALS<span></span> [<span><br>'b'</span><span> ];</span>}<span></span><span>Sum</span><span>();<br>echo </span><span>$b</span><span>;<br><br></span>?><span></span><span></span><span></span><span></span><span>$GLOBALS </span><span> is an associative array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the content of the variable. </span><span>$GLOBALS </span><span>Exists in the global scope because $GLOBALS is a superglobal variable. The following example shows the use of superglobal variables: </span><span></span>Example #3 Example demonstrating super global variables and scope <span></span><span></span><span><br><?php<br></span><span>function </span><span>test_<br>global</span><span></span>()<span>{<br>   </span><span>/ / Most of the predictions Defining variables is not "super", they need to use the 'global' keyword to make them available in the local scope of the function. </span> </span>

global

$HTTP_POST_VARS; echo $HTTP_POST_VARS[

'name'

];

// Superglobals are valid in any scope, they do not require 'global ' statement. Superglobals were introduced in PHP 4.1.0.       <span><span>echo <br></span>$_POST<span></span>[<span><span>'name'</span></span>];<span>}<br><br></span>?><span><br></span><span><span></span></span> <span> The above introduces the usage of the global keyword and $GLOBALS in PHP, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials. </span> <span> <br></span></span></span></span>

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