PHP Basic: Variable ScopeTag: basic, variable, variable scope Category: PHP Basicpost: 01 Mar 2008 read: 211 PHP Basic Data Type Step By Step Tutorial - Part 12: Variable scope is a context where the variable defined. In principle, variable in PHP have single scope. Variable scope is differentiated become three type, there are: - global variable - local variable - variable of static Global variableGlobal variable is a variable that is having a global character and it is able to be recognized and used by the entire part of script. In PHP, global variable which will be used in the function must have to declare first in global in its function. Pay attention to the example of using variable global in script as following:
To access the global variable, you can also use $GLOBALS array variable. $GLOBALS array is good for noting all global variable in script.
Local variableLocal variable is variable which defined in a function so that the variable have the character of can only recognize and used in function which variable declaration. Local variable can have the name of which equal to the global variable.
Static VariableStatic variable is variable which there's only in local scope of a function. Variable do not eliminate the last value after finish to be executed and leave the function. It means that the last value after the executing which saves in the variable is not change until it will be called again. Example of using variable without static at script as following:
In the script above, it indicates that the last value of $a variable do not be defended, and it back sets into 0. You can see the difference of it by seeing the next script which use static. Variable of static is usually used as counter. For example, in noting the amount of visitor at one particular web situs. The example of using variable with static at script as following:
|
| | Give Your Opinion | Recommend |
|

