PHP variable expression _3_2_

The following is learning Kong Xiangsheng editor of "PHP Programming Fundamentals tutorial and example" (second edition) notes made.

 

PHP variables can be divided into custom variables and predefined variables.

The following variables are talking about are custom variables.

Basic concepts of variable

  PHP variable names follow the following rules:

    (1) variable name must be a dollar sign ($) at the beginning, such as $ username.

    (2) the first character of the variable name must be a letter or an underline (not a number), the variable name may be letters, numbers and combinations of lower draw lines, such as $ user_name_1.

    (3) PHP variable names are case-sensitive. This means that $ userName and $ UserName is two completely different variables.

  And not the same as traditional high-level language, PHP can modify the value of the variable method by re-assigned to the variables already defined, and even modify the data type of the variable.

1 ? < PHP
 2  // The following statement modifies the value of the variable $ userName 
3  $ userName = "John Doe" ;
 4  $ userName = "John Doe" ;
 5  
6  // The following statement is either modify the value of $ sex variables, and modify the Sex $ variable data type 
. 7  $ Sex = FALSE ;
 . 8  $ Sex = "M" ;
 . 9  
10  echo  $ the userName ;
 . 11  echo "a" ;
 12 is  echo  $ Sex ;
 13 is >?

Output:

John Doe
male

 

2. The variable assignment mode

  Variable assignment means specific to the variable data, using the assignment operator '=' is achieved.

  PHP provides two ways assignment: assignment by value and pass the address assignment.

(1) by value assignment manner

  Assignment by value way to value a "copy" is assigned to a variable.  

. 1 ? < PHP
 2  $ AGE1 = 18 ;
 . 3  // The following statement assignment by value, variable value $ 18 AGE1 Age2 assigned to the variable $ 
. 4  $ Age2 = $ AGE1 ;
 . 5  // following statements modify variable values of $ Age2, at this time, the variable $ Age2 open space to store in memory the new value 20 is 
. 6  $ Age2 = 20 is ;
 . 7  echo  $ AGE1 ; // this statement AGE1 output variable value $ 18 is 
. 8  echo "a" ;
 . 9  echo  $ Age2 ; // this statement variable $ age2 output value of 20 
10 >?

Output:

18
20

Description:

① Program execution "  $ age2 = $ AGE1 the" memory and no new variable value $ age2 18, which is due to PHP in order to improve efficiency in the use of memory, uses the principle of "copy on write" the variable assignment. In short, unless there was a write (or modify), otherwise point to the same address of the variable value object will not be copied, so that is save memory and improve the efficiency of the code.

② the implementation of "$ age2 = 20;" the statement added only memory variable value variable $ age2 20.

 

(2) assign by way

  Biography address assignment is an assignment memory address of the source variable to a new variable, the new variable refers to the value of the source variable, change the value of the new variable will affect the value of the source variable, and vice versa. Biography address assignment means that both variables point to the same data, any data copy process does not exist. PHP through before appending the source variable ($ oldVariable) "&" symbol to achieve assign by reference, syntax is: $ newVariable = & $ oldVariable.

? < PHP
 $ age1 = 18 ;
 // passaged address assignment, the variable $ age1 address of (reference) Age2 assigned to the variable $ 
$ Age2 = & $ age1 ;
 $ Age2 = 20 ;
 echo  $ age1 ;      // This statement output $ AGE1 variable is 20 
echo "a" ;
 echo  $ Age2 ;      // this statement AGE1 output variable value $ 20 
>?

Output:

20
20

Description:

① the program is executed "$ age2 = & $ age1", the variable $ $ AGE1 Age2 variable points to the same memory 18 a variable value.

② after program execution "$ age = 20" statement, and the variable $ age2 variable $ age1 pointing to the memory of a variable with a value of 20.

 

3. Variable Variable

  PHP variable variable allows the program to dynamically change a variable name, variable, variable working principle is "value" as a variable to another variable "name."

. 1 ? < PHP
 2  $ varname = "Age" ;
 . 3  // substituted with $ age $$ varname. The following code is equivalent to: $ age = 20 is; 
. 4 $ $ varname = 20 is ;
 . 5  echo  $ age ; // output value of the variable $ age: 20 
. 6 >?

Output:

20

 

Guess you like

Origin www.cnblogs.com/xiaoxuStudy/p/11603262.html