Home >Backend Development >PHP Tutorial >PHP Expressions for PHP Learning_PHP Tutorial

PHP Expressions for PHP Learning_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:04:22849browse

PHP expression

Expression is the most important element of PHP. In PHP 3.0, almost everything you write is an expression. The simplest but precise definition of an expression is "anything that has 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 an integer constant). Here, you want to assign $a to 5. So when you write $b = $a, the desired result is $b = 5. That is, $a is an expression with a value of 5. A simple example of a complex expression is a function.
For example, consider the following function:   function foo()   {    return 5; A function is an expression whose value is its return value. Because foo() returns 5, the expression 'foo()' evaluates to 5 .
PHP’s values ​​are of course not limited to integers, and usually aren’t. PHP supports three types of values: integer values, floating point values ​​and string values. PHP supports two mixed types (non-scalar): arrays and objects. Values ​​of both types 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's easy to see that there are two values ​​here, the value of the integer 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 being assigned, which is 5 in this case. In fact, it means that regardless of what '$a = 5' does, it is an expression with a value of 5. Thus, writing statements such as '$b = ($a = 5)' is like '$a = 5; $b = 5;' (with a semicolon at the end of each statement). Because the order of assignment is from right to left, you can also write '$b = $a = 5'. ​

Another good example of the direction of expression evaluation is add first, add last and subtract first, then subtract. Users of PHP/FI and most other languages ​​are probably familiar with variable++ and variable--. This is the self-increment and self-decrement operation. In PHP/FI 2, the statement '$a++' has no value (it is not an expression), so you can neither assign to it nor use it in any way. PHP 3 turns them into the same expressions as in C, thereby enhancing the capabilities of auto-increment and auto-subtraction operations.
Similar to C, there are two types of self-add in PHP 3 - add first and add last. The essence of adding first and adding later is that the variables add themselves, and they have the same effect on the variables themselves. The difference is the value of the self-increasing expression. Add first in the form of '++$variable', calculate the value after the variable is added (PHP first adds the variable, and then read its value, which is also called 'add first'). Add in the form of '$variable++' Post-adding, the value of the original variable $variable is calculated first, and then self-adding is performed (PHP does self-adding after reading the value of the variable, so it is called 'post-adding').
The most common expression is comparison expression. This expression evaluates to 0 or 1, which means FALSE or TRUE respectively.
PHP supports > (greater than), >= (greater than or equal to), == (equal to), < (less than) and <= (less than or equal to). This kind of expression is usually used in conditional execution, such as IF statement.
The last expression we want to discuss here is the mixed assignment expression. You already know that if you want to increment $a, you can simply write '$a++' or '++$a'. But what if the value you want to increment is larger than 1, for example to make it increase by 3? You could write '$a++' a few more times, but this is obviously not an efficient or acceptable way. Another common approach is to write '$a = $a + 3'. First calculate the value of '$a + 3', and then assign it back to $a, so that $a is added to 3. In PHP 3, you can abbreviate it like you do in several other languages ​​(such as C), which makes it clearer, faster and easier to understand. Adding 3 to the current variable $a can be written as '$a += 3'. This sentence means "take the value of $a, add 3 to it, and assign it to $a". In addition to making the statement shorter and clearer, this 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 what is assigned to $a). Any double 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 expressions. Many times (mainly in conditional execution and looping), you don't care about the specific value of an expression, but only 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, zero is FALSE. It is important to note that the value of negative zero is non-zero and is considered TRUE! The empty string can be FALSE; all other strings are TRUE. For non-quantitative values ​​(arrays and objects) - FALSE if its value does not contain any elements, TRUE otherwise.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315992.htmlTechArticlePHP expression Expression is the most important element of PHP. In PHP 3.0, almost everything you write is an expression. The simplest but precise definition of an expression is anything that has a value...
Statement:
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