Home > Article > Backend Development > Comprehensive understanding of php expressions
is the most important thing about php. In php, almost everything you write is an expression. The simple but most precise way to define an expression is "anything with a value".
the basic. When you type "$a = 5", the value "5" is assigned to the variable $a. "5", obviously, its value is 5, in other words "5" is an expression with a value of 5 (here, "5" is an integer constant).
After assignment, the expectation is that $a has a value of 5, so if we write $b = $a, the expectation is that it will be the same as $b = 5. In other words, $a is an expression that also evaluates to 5. If everything works correctly, this is exactly what is going to happen.
For example, consider the following function:
<?php function foo (){ return 5; } ?>
Assuming you are already familiar with the concept of functions (if not, take a look at the relevant chapter on functions), then type $ c = foo() is essentially like writing $c = 5, which is true. Functions are also expressions, and the value of an expression is their return value. Since foo() returns 5, the expression "foo()" also evaluates to 5. Often functions don't just return a static value, but may compute something.
Expression classification
A commonly used expression type is a comparison expression. These expressions evaluate to FALSE or TRUE. PHP supports > (greater than), >= (greater than or equal to), == (equal to), != (not equal to), < (less than), <= (less than or equal to). PHP also supports the congruent operator === (the values and types are the same) and the non-congruent operator !== (the values or types are different). These expressions are most commonly used in conditional judgment statements, such as if statements.
Here, the last example that will be studied is the combined operation assignment expression. Already know that if you want to add 1 to the variable $a, you can simply write "$a++" or "++$a". But what if you want to add a value greater than 1 to a variable, say 3? You can write "$a++" multiple times, but this is obviously not an efficient and comfortable method. A more general approach is "$a = $a + 3". "$a + 3" computes the value of $a plus 3, and reassigns the resulting value to variable $a, so $a's value is increased by 3. In PHP and several other C-like languages, the above functions can be accomplished in a shorter form, and therefore more clearly and quickly. To add 3 to the current value of $a, you can write: "$a += 3". What it means here is "take the value of variable $a, add 3, and assign the result to variable $a again." In addition to being simpler and clearer, it can also run faster. The value of "$a += 3", like the value of a normal assignment operation, is the value after assignment. Note that it is not 3, but the value of $a plus 3 (this value will be assigned to $a). Any binary operator can use the operation assignment pattern, such as "$a -= 5" (subtract 5 from the value of variable $a), "$b *= 7" (multiply variable $b by 7), etc. wait.
There is another expression, which may look strange if you have not seen it in other languages, namely the ternary conditional operator:
$first ? $second : $third
If the first subexpression The value of the expression is TRUE (non-zero), then the value of the second subexpression is calculated, and its value is the value of the entire expression. Otherwise, it will be the value of the third subexpression.
The above is the detailed content of Comprehensive understanding of php expressions. For more information, please follow other related articles on the PHP Chinese website!