Home >Backend Development >PHP Tutorial >A brief analysis of the scope and life cycle of constants, variables in PHP_PHP Tutorial
Variables in PHP scripts mainly include: built-in super global variables, general variables, constants, global variables, static variables, etc. When we use them, in addition to correctly knowing their syntax, what is more important is , we need to know their essential differences and connections - that is, the issue of their scope.
1. Built-in super global variables can be used and visible anywhere in the script. That is to say, if we change one of the values in a PHP page, its value will also change when used in other PHP pages.
2. Once constants are declared, they will be globally visible, that is, they can be used inside and outside functions, but this is only limited to PHP scripts included in one page (including the PHP scripts we include through include and include_once), but in other It cannot be used in the page.
3. Global variables declared in a script are visible throughout the script, but not inside the function. If the variable inside the function has the same name as the global variable, the variable inside the function shall prevail.
4. When the variables used inside the function are declared as global variables, their names must be consistent with the names of the global variables. In this case, we can use the global variables outside the function in the function, so that we can avoid The previous situation is that the external variable is overwritten because the variable inside the function has the same name as the external global variable.
5. Variables created and declared as static inside a function cannot be visible outside the function, but the value can be maintained during multiple executions of the function. The most common situation is during the recursive execution of the function.
6. Variables created inside a function are local to the function, and when the function terminates, the variable no longer exists.
The complete list of super global variables is as follows:
1.$GOBALS Array of all global variables
2.$_SERVER Array of server environment variables
3. $_POST Variable array passed to the script through the POST method
4.$_GET Variable array passed to the script through the GET method
5.$_COOKIE Cookie variable array
6.$_FILES Related to file upload Variable array
7.$ENV environment variable array
8.$_REQUEST Variable array entered by all users including input content contained in $_GET $_POST $_COOKIE
9.$_SESSION session variable array
What we should pay attention to is: Another important difference between variables and constants is: constants can only define boolean (Boolean), integer (integer), float (float) Point type) and string (string type) data, but resource type data cannot be defined.