PHP function related --- global variables and local variables

1. Pass by value by reference


      At this time, the actual parameter is not passed the value, but the address pointed to by the actual parameter;

[php]  view plain copy  
  1. <?php  
  2.   
  3.     $a = 18;  
  4.       
  5.     function  aaa(& $num ){     // Note that there is an "&" symbol before the parameter at this time, which is an address symbol, and the passed value is not the value corresponding to the parameter, and the second is the address pointed to by the parameter;   
  6.           
  7.       return$num += 1;    
  8.     }  
  9.       
  10.     echo $a ;              //18 When the function is not called, output the original value directly;   
  11.       
  12.     echo  aaa( $a );         //19 Call the function, pass the address of $a, execute the statement in the function body, and return;  
  13.       
  14.     echo $a ;              //19 Since the content of the address pointed to by $a has changed, it is called by the function, and $a has also changed;   
  15.   
  16. ?>  


2. The global keyword:


    The global keyword is used before a variable, that is, a declaration: this variable is used to represent a global variable (used inside a function);

[php]  view plain copy  
  1. <?php  
  2.   
  3.     $gg = 28;  
  4.       
  5.     function bbb(){  
  6.           
  7.         global $gg ;                   // Note: At this time, the "global" keyword is used before the variable name, and the variable is declared using $gg in the "global scope";   
  8.           
  9.         return$gg -= 2;   
  10.     }  
  11.       
  12.     echo $gg ;             // 28 ; When the function is not called, the original value is output as it is;   
  13.       
  14.     echo  bbb();          // 26; The function is called, the statement in the function body is executed, and the keyword global is declared before the variable, indicating that the variable used (represented) at this time is a global variable;  
  15.   
  16.     echo $gg ;           // 26; Since the global variable is used when the function is executed, the global variable $gg has also changed;   
  17.   
  18. ?>  


3. Super global variables:


    (1) Super global variables can be accessed and used anywhere in the script, any layer of functions, any corner;

    (2), the super global variables mainly include $GLOBALS, $_GET, $_POST.... They are all displayed in the form of arrays;

[php]  view plain copy  
  1. <?php  
  2.   
  3.      $a = 23;  
  4.        
  5.      $b = 55;  
  6.        
  7.      print_r ( $ GLOBALS );  
  8.   
  9. ?>       

The above code is executed, and some of the print results are: [a] => 23; [b] => 55; That is: in the $GLOBALS variable, all global variables in the page are stored; variable name ($a, $b) for

The global variable variable name ($a, $b) is the health name in the superglobal variable $GLOBAL ([a], [b]);

[php]  view plain copy  
  1. <?php  
  2.   
  3.      $a = 23;  
  4.        
  5.      $b = 55;  
  6.        
  7.      function ccc(){  
  8.            
  9.          $ GLOBALS [ 'a' ] + = 2;  
  10.                              // Note: There is no $a in the function at this time; that is, return $a; will report an error. At this time, the $a that returns return should be the $a inside the function, and it returns suddenly without any declaration at all. (so an error will be reported), and the previous sentence is the executed super-global variable, which changes the global variable $a outside the function;  
  11.      }  
  12.        
  13.        
  14.      echo $a ;   //23   
  15.    
  16.      echo  ccc();    // There is no return value at this time, only super global variables are used, executed and changed in the function;  
  17.   
  18.      echo $a ;    //25 The super global variable, that is, the variable itself, but the change is executed inside the function body;   
  19.   
  20. ?>  


4. Note: Once the constant is declared, it can also be referenced in any position in the function or script;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325483862&siteId=291194637