PHP Variable Summary

web front end six thousand one hundred and fifty-eight 13 years ago (2011-04-27)

PHP variables always start with the dollar sign $, followed by the variable name. The variable name follows the naming rule of identifier, that is, the variable name can start with a letter or underscore, and consists of letters, underscores, numbers, or other ASCII characters from 127-255. Note that variables are case sensitive.
PHP does not need to display declaration variables. Variable declaration can be performed simultaneously with assignment. A good programming habit is that all variables should be declared before use, preferably with comments.
1、 Assignment of variables
After a variable is declared, it can be assigned in two ways: value assignment and reference assignment.
1. Value assignment
        $color = "red" ;
        $sum = 12+"15" ;
2. If you want two variables to point to the same copy of a value, you need to assign a value by reference.
Reference assignment
The created variable refers to the same content as another variable. If multiple variables refer to the same content, modify any one of them, and it will be reflected in the rest of the variables.
        <? php
                     $value1 = "hello"  ;
                     $value2 = &value1 ;       
                     $value2 = "goodbye" ;   
2、 Scope of variable
  PHP script You can declare variables anywhere in the, but the location of the declaration variable will greatly affect the scope of the access variable. This accessible scope is called scope.
Four scopes of PHP variables:
△ Local variable
△ function parameter
△ Global variable
△ Static variable
1. Local variable
A variable declared in a function is considered a local variable and can only be referenced in the function. When you exit a function declaring a variable, the variable and its corresponding value will be revoked. It eliminates the possibility that variables that cause global access are modified intentionally or unintentionally.
        $x = 4 ;
        function assignx ( ) {
                      $x = 0 ;
                      print "\$x inside function is $x . <br>" ;
        assignx ( ) ;
        print "\$x outside of function is $x . <br>" ;
The code execution result is:
        $x inside function is 0 .
        $x outside function is 4 .
2. Function Parameters
PHP, like other programming languages, any function that accepts parameters must declare these parameters in the function header. Although these parameters accept values outside the function, they cannot be accessed after exiting the function. (except parameters passed by reference)
For example:
                      function x10 ( $value ) {
                                $value = $value * 10 ;
                                return $value ;
After the function is executed, the parameters will be revoked.
3. Global Variables
In contrast to local variables, global variables can be accessed anywhere in the program. When changing a global variable in a function, you need to display the variable as a global variable in the function by adding GLOBAL to the variable in the function.
For example:
                  $somevar =  15 ;
                  function  addit ( ) {
                             GLOBAL $somevar ;
                             $somevar ++ ;
                             print "somevar is $somevar" ;
                  addit ( ) ;
The displayed value of $somevar should be 16. However, if GLOBAL $somevar is removed; In this line, the variable $somevar will be implicitly set to 0, plus 1, and the last displayed value is 1.
Another way to declare global variables is to use PHP's $GLOBAL array, as follows:
                 $somevar = 15 ;
                 function addit ( ) {
                         $GLOBALS[ 'somevar' ]++ ;
                 addit ( ) ;
                 print "somevar is ". $ GLOBALS[ 'somevar' ] ;
The return value is as follows: somevar is 16
4. Static variable
Static scope. The function parameters of ordinary variables will be revoked at the end of the function, but the static variables will not lose their values when the function exits, and the values can be retained when the function is called again. You can declare a static variable by adding the keyword STATIC before Bianliang Ming.
  STATIC $somevar ;
Consider an example:
        function keep_ track ( ) {
                   STATIC $count = 0 ;
                   $count ++ ;
                   print $count ;
                   print "<br>" ;
         keep_ track ( ) ;
         keep_ track ( ) ;
         keep_ track ( ) ;
         keep_ track ( ) ;
If $count is not specified as static (correspondingly, $count is a local variable), the output will be
Because $count is static, it will retain the previous value each time the function is executed. The output is as follows:
Static scopes are useful for recursive functions. Recursive function is a powerful programming concept. It is a function that can repeatedly call itself until a certain condition is met.
5. PHP Super Global Variables
PHP provides a lot of useful predefined variables, which can be accessed at the person and location executing the script, and is used to provide a lot of environment related information. These variables can be used to obtain detailed information about the current user session, user operating system environment, and local operating environment. PHP will create some variables, while the availability and values of many other variables depend on the operating system and Web services.
Output all predefined variables:
        foreach ( $_SERVER as $var => $value ) {
                     echo  "$var => $value <br>" ;
Display the user's IP address:
        print "HI!Your IP address is ".$_ SERVER[ ' REMOTE_ADDR' ] ;
To use a predefined variable array in PHP, you must use it in PHP Enable the configuration parameter track in the INI file_ vars 。