Home > Backend Development > PHP Tutorial > PHP global variable scope analysis_PHP tutorial

PHP global variable scope analysis_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:45:08
Original
831 people have browsed it

Copy code The code is as follows:

$a = 1;
include 'b.inc' ;
?>

This variable $a will take effect in the include file b.inc. However, in user-defined functions, a local function scope will be introduced. Any variables used inside a function will be restricted to the local function scope by default.
Copy code The code is as follows:

$a = 1; /* global scope */
function Test ()
{
echo $a ; /* reference to local scope variable */
}
Test ();
?>

This script will produce no output because the echo statement refers to a local version of the variable $a, and it is not assigned a value within this scope. You may notice that global variables in PHP are a little different from C language
Global variables in PHP must be declared as global (global keyword) when used in functions
Copy code The code is as follows:

$a = 1;
$b = 2;
function Sum ()
{
global $a , $b ;
$b = $a + $b ;
}
Sum ();
echo $b ;
?>

The output of the above script will be "3".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320427.htmlTechArticleCopy the code as follows: ?php $a = 1; include 'b.inc'; ? The variable $a here Will take effect in the include file b.inc. However, in a user-defined function, a local function scope will...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template