Copy the code The code is as follows:
$a = 1;
include 'b.inc';
?>
Here the variable $a will be included in the file b.inc takes effect in. 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 not produce any output because the echo statement refers to a local version of the variable $a, and it is not assigned a value in this scope. You may notice that the global variables in PHP are a little different from the C language. Global variables in PHP must be declared as global (global keyword) when used in functions. Copy the code
The code is as follows:
< ?php $a = 1; $b = 2;
function Sum ()
{
global $a, $b;
$b = $a + $b;
}
Sum ();
echo $b ;
?>
The output of the above script will be "3".
The above introduces the global variable PHP global variable scope analysis, including the content of global variables. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement: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