Variables defined within the program are called
Local Variables, which is used only within the program and disappears as the program ends. Variables that are defined outside the program are called
Global Variables, can be called by the program, but will not disappear with the end of the program. The previous chapters describe the local variables of the PHP function's custom function and the global variables of the custom function. Define a function The variables in this function are local variables, they can only be used inside this function return is the function of the return value call this function will output this value can be the function of the variable is a local variable. A global variable is a variable that is defined outside the function, and its scope is defined at the end of the file.
In PHP, functions are treated as separate patches, so local variables overwrite global variables, even if they are in local variables and define variables that do not have the same global variables, they are overwritten.
Here is an example of how they differ:
<?php//local variable Test $s1= "out S1"; Global variable function say () {$s 1 = "in S1";//local variable echo "Say (): $s 1";} Say (); Output local variables: in S1echo "<br/>", echo "function out: $s 1"; Output global variable: Out s1//static variable test function count1 () {$num = 0; $num + +; echo $num. " ";} function Count2 () {static $num = 0; $num + +; echo $num. " ";} for ($i =0; $i <10; $i + +) {count1 ();//11 1 1 1 1 1 1 1 1 1}echo "<br/>"; for ($i =0; $i <10; $i + +) {count2 ();//1 2 3 4 5 6 7 8 9 10}echo "<br/>";//The global variable is used in the function, plus global$a= "php"; $b = "Java"; function show () {echo $a;//no output global $b; E Cho $b; Define Global, Output java}show ();? >
Description of the difference between local and global variables:
1. Local variables inside the function can access internal variables, output results; the variables inside the function cannot be accessed outside the function, so the result cannot be output, and if the value of the variable needs to be called outside the function, it must be passed back to the main program block for subsequent processing by the return instruction.
2. In PHP, because a function can be treated as a separate program snippet, the local variable overrides the visibility of the global variable, so the global variable cannot be called directly in the function. To use a global variable in a function, you must define the target variable with the keyword "global", and with the keyword "global" You can import the global data into the local scope of a function to tell the function body that the variable is a global variable.
3. Another important feature of the scope of variables in PHP is the static variable (the statics variable). Static variables exist only in the local function domain and are initialized only once, and when the program executes away from this scope, its value does not disappear, and the result of the last execution is used. The above examples use static and static variables separately, and friends can compare them carefully.
"Related tutorials Recommended"
1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"
2. PHP programming from getting started to mastering the full set of video tutorials
3. PHP real-Combat video tutorial