PHP expressions
Expressions are the most important constituent elements of PHP. In PHP 3.0, almost anything you write is an expression. The simplest but precise definition of an expression is "anything with a value". A simple example is constants and variables.
When you write "$a = 5", you assign the value '5' to $a. (In this case, '5' is a plastic surgery constant). Here, you want to assign $a to 5. So when writing $b = $a, the result you want is $b = 5. That is, $a is an expression with a value of 5. A simple example of complex expressions is a function.
For example, consider the following function: function foo() { return 5; }
If you think writing $c = foo() is actually the same as writing $c = 5, then you are right. A function is an expression whose value returns it. Because foo() returns 5, the value of the expression 'foo()' is 5 .
The value of PHP is of course not limited to plastic surgery, and is usually not. PHP supports three types of values: shaping values, floating point values and string values. PHP supports two mixed types (non-scalar): arrays and objects. These two types of values can be assigned to variables or returned from functions.
PHP 3 is an expression-oriented language, so almost everything is an expression.
Consider the example we have discussed, '$a = 5'. It is easy to see that there are two values here, the value of the shaping constant '5', and the value of the variable $a, which is also assigned to 5. But there is actually an additional value here, which is the value of the assignment statement itself.
The value of the assignment statement itself is the value assigned to, in this case 5. In fact, it means that it does not consider what '$a = 5' is an expression with a value of 5. In this way, write a statement such as '$b = ($a = 5)', just like '$a = 5; $b = 5;' (there is a semicolon at the end of each statement). Because the order of assignments is from right to left, you can also write it as '$b = $a = 5'.
Another good example of expression calculation direction is adding first, then adding, and subtracting first, and then subtracting. Users of PHP/FI and most other languages may be familiar with variable++ and variable--. This is the self-addition and self-decrease operation. In PHP/FI 2, the statement '$a++' has no value (it is not an expression), so you can neither assign value to it nor use it in any way. PHP 3 turns them into the same expression as in C, thus enhancing the ability of self-addition and self-deduction operations.
Similar to C, there are two types of self-addition in PHP 3 - add first and then add. The essence of adding first and then adding is that variables are added by themselves, and their effects on the variables themselves are the same. The difference is the value of the self-added expression. The first addition of the shape like '++$variable' is calculated, and the value after the variable is added (PHP first does the variable self-addition, and then read its value, which is called 'add first'). The second addition of the shape like '$variable++' is calculated, and then the value of the original variable $variable is calculated first, and then the value of the self-addition is performed (PHP reads the variable value and then does the self-addition, so it is called 'addition after the value of the variable is added).
The most common expression is comparison expression. This expression is calculated as 0 or 1, which means FALSE or TRUE, respectively.
PHP supports > (greater than), >= (greater than or equal to), == (equality), < (less than) and <= (less than or equal to). This expression is usually used in conditional execution, such as IF statements.
The last expression we want to discuss here is a mixed assignment expression. You already know that if you want to add $a to one, you can simply write a sentence '$a++' or '++$a'. But what if the value you want to increase is larger than 1, for example, to add 3 ? You can write '$a++' several times, but this is obviously not an efficient or acceptable way. Another common way is to write '$a = $a + 3'. First calculate the value of '$a + 3', and then return to $a, so $a will add 3. In PHP 3, you can abbreviate it like in several other languages (such as C) so that it is clearer, faster, cleaner and easier to understand. Adding the current variable $a to 3 can be written as '$a += 3'. This sentence means "take out the value of $a, add it to 3, and assign it to $a". This not only makes the statement short and clear, but also makes it execute faster. The value of the expression '$a += 3', like a strict assignment statement, is the assigned value. Note: It is not 3, but the value of $a plus 3 (this is assigned to $a). Any dual operator can be used in this assignment mode, such as '$a -= 5' (variable $a minus 5), '$b *= 7' (variable $b multiplied by 7), etc.
The last thing worth mentioning is the truth value of the expression. Many times (mainly when conditional execution and looping) you don't care about the specific value of the expression, but just notice whether it represents TRUE or FALSE (PHP does not have a dedicated Boolean type). PHP uses a perl-like method to calculate the truth value of an expression. Any non-zero value is TRUE and zero is FALSE. Please be sure to note that the value of negative zero is a non-zero value and is considered TRUE! The empty string can be FALSE with the string "0"; so other strings are TRUE. For non-numeric values (arrays and objects) - if its value does not contain any elements, TRUE otherwise.
Expressions are the most important constituent elements of PHP. In PHP 3.0, almost anything you write is an expression. The simplest but precise definition of an expression is "anything with a value". A simple example is constants and variables.
When you write "$a = 5", you assign the value '5' to $a. (In this case, '5' is a plastic surgery constant). Here, you want to assign $a to 5. So when writing $b = $a, the result you want is $b = 5. That is, $a is an expression with a value of 5. A simple example of complex expressions is a function.
For example, consider the following function: function foo() { return 5; }
If you think writing $c = foo() is actually the same as writing $c = 5, then you are right. A function is an expression whose value returns it. Because foo() returns 5, the value of the expression 'foo()' is 5 .
The value of PHP is of course not limited to plastic surgery, and is usually not. PHP supports three types of values: shaping values, floating point values and string values. PHP supports two mixed types (non-scalar): arrays and objects. These two types of values can be assigned to variables or returned from functions.
PHP 3 is an expression-oriented language, so almost everything is an expression.
Consider the example we have discussed, '$a = 5'. It is easy to see that there are two values here, the value of the shaping constant '5', and the value of the variable $a, which is also assigned to 5. But there is actually an additional value here, which is the value of the assignment statement itself.
The value of the assignment statement itself is the value assigned to, in this case 5. In fact, it means that it does not consider what '$a = 5' is an expression with a value of 5. In this way, write a statement such as '$b = ($a = 5)', just like '$a = 5; $b = 5;' (there is a semicolon at the end of each statement). Because the order of assignments is from right to left, you can also write it as '$b = $a = 5'.
Another good example of expression calculation direction is adding first, then adding, and subtracting first, and then subtracting. Users of PHP/FI and most other languages may be familiar with variable++ and variable--. This is the self-addition and self-decrease operation. In PHP/FI 2, the statement '$a++' has no value (it is not an expression), so you can neither assign value to it nor use it in any way. PHP 3 turns them into the same expression as in C, thus enhancing the ability of self-addition and self-deduction operations.
Similar to C, there are two types of self-addition in PHP 3 - add first and then add. The essence of adding first and then adding is that variables are added by themselves, and their effects on the variables themselves are the same. The difference is the value of the self-added expression. The first addition of the shape like '++$variable' is calculated, and the value after the variable is added (PHP first does the variable self-addition, and then read its value, which is called 'add first'). The second addition of the shape like '$variable++' is calculated, and then the value of the original variable $variable is calculated first, and then the value of the self-addition is performed (PHP reads the variable value and then does the self-addition, so it is called 'addition after the value of the variable is added).
The most common expression is comparison expression. This expression is calculated as 0 or 1, which means FALSE or TRUE, respectively.
PHP supports > (greater than), >= (greater than or equal to), == (equality), < (less than) and <= (less than or equal to). This expression is usually used in conditional execution, such as IF statements.
The last expression we want to discuss here is a mixed assignment expression. You already know that if you want to add $a to one, you can simply write a sentence '$a++' or '++$a'. But what if the value you want to increase is larger than 1, for example, to add 3 ? You can write '$a++' several times, but this is obviously not an efficient or acceptable way. Another common way is to write '$a = $a + 3'. First calculate the value of '$a + 3', and then return to $a, so $a will add 3. In PHP 3, you can abbreviate it like in several other languages (such as C) so that it is clearer, faster, cleaner and easier to understand. Adding the current variable $a to 3 can be written as '$a += 3'. This sentence means "take out the value of $a, add it to 3, and assign it to $a". This not only makes the statement short and clear, but also makes it execute faster. The value of the expression '$a += 3', like a strict assignment statement, is the assigned value. Note: It is not 3, but the value of $a plus 3 (this is assigned to $a). Any dual operator can be used in this assignment mode, such as '$a -= 5' (variable $a minus 5), '$b *= 7' (variable $b multiplied by 7), etc.
The last thing worth mentioning is the truth value of the expression. Many times (mainly when conditional execution and looping) you don't care about the specific value of the expression, but just notice whether it represents TRUE or FALSE (PHP does not have a dedicated Boolean type). PHP uses a perl-like method to calculate the truth value of an expression. Any non-zero value is TRUE and zero is FALSE. Please be sure to note that the value of negative zero is a non-zero value and is considered TRUE! The empty string can be FALSE with the string "0"; so other strings are TRUE. For non-numeric values (arrays and objects) - if its value does not contain any elements, TRUE otherwise.