What is an expression in PHP?

Source: Internet
Author: User
Tags scalar type casting
Expressions are the most important cornerstone of PHP. In PHP, almost anything written is an expression. The simplest but most precise way to define an expression is "anything that has value."

The most basic form of expression is constants and variables. When you type "$a = 5", the value "5" is assigned to the variable $a. "5", it is obvious that its value is 5, in other words "5" is an expression with a value of 5 (Here, "5" is an integer constant).

After the assignment, the expected condition is that the value of the $a is 5, so if you write down $b = $a, you expect it to be like $b = 5. In other words, $a is an expression that has a value of 5. If everything works correctly, that's exactly what's going to happen.

A slightly more complex example of an expression is a function. For example, consider the following function:

<?phpfunction  foo  () {    return  5;}? >

Assuming you are already familiar with the concept of a function (if not, take a look at the relevant section of the function), then typing $c = foo () is essentially like writing down $c = 5, which is true. The function is also an expression, and the value of the expression is their return value. Since Foo () returns 5, the value of the expression "foo ()" is also 5. Normally a function does not simply return a static value, but it may calculate something.

Of course, the values in PHP are often not integral types. PHP supports four scalar values (scalar values cannot be split into smaller units, for example, and arrays of different) types: integer value (integer), floating-point value (float), string value (String), and Boolean value (Boolean). PHP also supports two kinds of composite types: arrays and objects. Both types can be assigned to variables or returned from functions.

PHP, like any other language, develops on the path of expression, but pushes it far further. PHP is an expression-oriented language, and in this respect almost everything is an expression. Consider the example that has just been studied, "$a = 5". It is clear that there are two values, the value of the integer constant 5, and the value of the variable $a, which is also updated to 5. But the fact is that there is an additional value, the value of the attached statement itself. The assignment statement itself evaluates to the assigned value, which is 5. In fact, this means "$a = 5", not necessarily what it does, but an expression with a value of 5. Thus, write "$b = ($a = 5)" and write "$a = 5; $b = 5 "(The semicolon marks the end of the statement) is the same. Because the order of the assignment is from right to left, you can also write "$b = $a = 5".

Another good example of expression-oriented expressions is the increment and decrement of the front and back. Users of PHP and most other languages should be more familiar with variable + + and variable-symbol. That is, the increment and decrement operators. In Php/fi 2, the statement "$a + +" has no value (not an expression), so you cannot assign it to it or use it in any other way. PHP enhances the ability to increment/decrement by turning it into an expression, similar to the C language. In PHP and C, there are two types of increment before and after increment, essentially, both pre-increment and post-increment both increase the value of the variable and have the same effect on the variable. The difference is the value of the increment expression. Before incrementing, write "+ + $variable", to increase the value (PHP before reading the value of the variable, increase the value of the variable, so called "pre-increment"). After increment, write "$variable + +", the original value of the variable is not incremented (PHP after reading the value of the variable, increase the value of the variable, so called "after Increment").

A common-to-expression type is a comparison expression. These expressions are evaluated as FALSE or TRUE. PHP Support > (greater than), >= (greater than or equal), = = (equals),! = (not equal to),< (less than), <= (less than equals). PHP also supports the congruent operator = = = (both values and types are the same) and non-congruent operators!== (values or types differ). These expressions are most commonly used in conditional judgment statements, such as the IF statement.

Here, the last example that will be studied is the combination of the 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 for a variable, such as 3? You can write "$a + +" multiple times, but this is obviously not an efficient and comfortable method, and a more general practice is "$a = $a + 3". "$a + 3" calculates the value of $a plus 3, and the resulting value is re-assigned to the variable $a, so the value of $a increases by 3. In PHP and several other C-like languages, this functionality can be done in a shorter form, and thus more clearly and quickly. Add 3 to the current value of the $a, and you can write this: "$a + = 3". This means "take the value of the variable $a, add 3, and the resulting result is assigned to the variable $a". In addition to being more brief and clear, it can be run faster. The value "$a + = 3", like the value of a normal assignment operation, is the value after the assignment. Note that it is not 3, but the value after the $a plus 3 (this value will be assigned to $a). Any binary operator can use an operation assignment pattern, such as "$a-= 5" (minus 5 from the value $a the variable), "$b *= 7" (Variable $b multiplied by 7), and so on.

There is also an expression that, if not seen in another language, may seem strange, namely the ternary conditional operator:

$first? $second: $third

If the value of the first subexpression is TRUE (not 0), then the value of the second subexpression is computed, and its value is the value of the entire expression. Otherwise, it will be the value of the third sub-expression.

The following example should generally help to understand pre-and post-increment and expression:

<?phpfunction  Double ($i) {    return  $i * 2;} $b  =  $a  =  5;         $c  =  $a + +;           $e  =  $d  = + + $b;      $f  =  double ($d + +);   $g  =  double (+ + $e);   $h  =  $g  + =  ten;       ? >

Some expressions can be used as statements. At this point, the form of a statement is ' expr '; ', that is, an expression and a semicolon end. In "$b = $a = 5;" , $a =5 is a valid expression, but it is not a statement in itself. "$b = $a = 5;" is a valid statement.

The last thing worth mentioning is the truth of the expression. In many cases, it is largely in terms of condition execution and looping that you do not focus on the explicit values in the expression, but pay attention to whether the value of the expression is TRUE or FALSE. The constant TRUE and FALSE (case-insensitive) are two possible Boolean values. If necessary, an expression is automatically converted to a Boolean. See the section on type casting.

PHP provides a complete set of powerful expressions, and providing it with complete documentation is beyond the scope of this manual. The above example should give you a good idea of what an expression is and how to build a useful expression. In the remainder of this manual, we will always use expr to represent a valid PHP expression.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.