Home > Backend Development > PHP Tutorial > scope of php variables

scope of php variables

angryTom
Release: 2023-04-07 08:38:01
Original
5960 people have browsed it

scope of php variables

Scope, a programming concept. Generally speaking, the name used in a piece of program code is not always valid/available, and the availability of the name is limited. The code scope is the scope of this name. Below we will introduce to you the scope of PHP variables.

Recommended tutorial: php introductory video tutorial

##In simple terms, there are 3 functions Domain: ## 

Local scope Global scope Super global scope

There are three types of variables:

Local variables Global variables Super Global variables

Local scope and local variables
are the scope within the function, in which the definition Variables are local variables (including formal parameters are also local variables).

Local variables can only be accessed within their functions.

Local variables will be automatically destroyed when the function call ends (it can be understood that the function execution ends and the execution space is also destroyed).

It can be seen that internal local variables cannot be accessed outside the function.

Static variable: a special local variable

Meaning:

Within the function, use the static keyword to modify the variable.

Form:

  function  XXX( .... ){
                            static  $s1 = 10;  //此时,$s1就是静态变量
                           ......
                      }
Copy after login

Characteristics of static variables:

The value of static local variables will not be destroyed at the end of the function call, but will Always reserved.

 

Contrast:

Non-static local variables will be destroyed at the end of the function call! In other words, when the function is called again, the variable (value) can continue to be used.

Global scope and global variables

It is the scope of the external scope of the function, and the variables defined in it are global variables.

Global variables can only be used (accessed) directly in the global scope in which they are located.

Super global scope and super global variables

The entire scope including local scope and global scope.

Superglobal variables can be used (accessed) in all scopes.

Actually, only a limited number of 10 system predefined variables are super global variables, including: $_GET, $_POST, $_REQUEST, etc.

Therefore, system predefined variables are also collectively called superglobal variables.

A special super global variable: $GLOBALS

It is also an array that stores all the global variables we define ourselves.

The variable name of each global variable is a unit of the $GLOBALS array.

For example:

Define the following variables in the global scope:

$v1 = 1;   //这一行执行,就有了一个这个:$GLOBALS[‘v1’], 其值为1
$v2 = ‘abc’;  //这一行执行,就有了一个这个:$GLOBALS[‘v2’], 其值为’abc’
$v3 = true;  //这一行执行,就有了一个这个:$GLOBALS[‘v3’], 其值为true
Copy after login

Function:

It allows us to use the global scope in the local scope variable.

The demonstration is as follows:

 $v1 = 10;  //全局变量
               function  func1(  ){
                          echo  $GLOBALS[‘v1’]; //输出10;
                          echo  $v1;    //报错:变量v1未定义
                          $s1 = $GLOBALS[‘v1’] * 5; //结果为50;
                         $s2 = $v1 * 5;    //报错:变量v1未定义
              }
Copy after login

A special keyword: global

Function:

Used in local scope , modify a local variable with the same name as the global variable.

At this time, the local variable can also use the value of the global variable - in fact, they are similar to the variable reference relationship.

Its function is similar to: $GLOBALS

   $a = 10;
      function sum(){
          global $a ;  //全局变量的引用的别名
          $a++;
          $b = $a + 1;
          echo $b;
      } 
      sum(); //  12
      echo $a ; //  11
Copy after login

The above is the detailed content of scope of php variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template