php variable scope
Jun 28, 2018 am 09:18 AM* There are only three scopes:
* 1. Global: Created outside the function, only used in the current script except for the function;
* 2. Local: Created inside the function, can only be used in the function, not accessible externally;
* 3. Static: Created inside the function, only used in the function, its value will not be lost after the function is executed;
1 |
|
//The global variable automatically becomes a key-value pair in the global variable array, and the key corresponds to the variable
1 |
|
* Function: It is a code segment with a specific function in the script, which can be called repeatedly
* 1. Basic syntax:
* 1.1 Function declaration: function funcnName($args){ #code...}
* 1.2 Function expression: $funcName = function ( $ages){ #code...}
* 2. Call:
* 2.1 Call by name: funcName($args) / $funcName($args)
* 2.2 Self-calling: declaration and calling are completed at the same time
* (function (args){ #code...})()
1 2 3 4 5 6 7 8 9 |
|
//Static variables must and can only be used in Declared and used in the function
1 2 3 4 5 6 7 |
|
//After the first execution is completed, the value of $num is 2
1 |
|
//After the second execution is completed, $ The num value is 3
1 |
|
//After the third execution, the $num value is 4
1 |
|
* Super global variables: $_SERVER, $_COOKIE, $_SESSION, $_GET, $_POST ,$_REQUEST
* 1. They are predefined variables, all are arrays, and can be used as they come, no declaration is required;
* 2. Cross-scope, both globally and locally (inside the function) ) can be used directly;
* 3. Cross-scope is not cross-script. The so-called super global, including global, refers to the current script file.
1 |
|
//Can be referenced directly globally
1 |
|
//Can also be referenced directly in the function
1 2 3 4 5 |
|
//Call the function
1 |
|

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP Notice: Undefined variable:Solution

Solution to PHP Notice: Undefined variable: result

PHP Notice: Undefined variable: arr in solution

How to pass PHP variables by reference

PHP Notice: Undefined variable: sql solution