PHP expression _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP expressions. Expressions are the most important cornerstone of PHP. In PHP, almost everything you write is an expression. The simplest but most accurate way to define an expression is "anythingtha expression is the most important cornerstone of PHP. In PHP, almost everything you write is an expression. The simplest but most accurate way to define an expression is "anything that has a value ".

The most basic expressions are constants and variables. When you type "$ a = 5", assign the value '5' to the variable $. '5'. Obviously, the value is 5. in other words, '5' is an expression with a value of 5 (in this case, '5' is an integer constant ).

After a value is assigned, you expect the value of $ a to be 5. Therefore, if you write $ B = $ a, you expect it to be like $ B = 5. In other words, $ a is an expression with a value of 5. If everything runs correctly, this is the correct result that will happen.

An example of a slightly complex expression is a function. For example, consider the following functions:

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

Suppose you are familiar with the concept of a function (if not, take a look at the relevant chapter of the function), then type $ c = foo () in essence, it is like writing $ c = 5, and you are correct. Functions are also expressions, and the values of expressions are their return values. Since foo () returns 5, the value of the expression 'foo () 'is also 5. Generally, a function does not return only a static value, but may calculate something.

Of course, values in PHP are often not integer. PHP supports three scalar value types: integer, floating point, and string values (scalar values cannot be split into smaller units, such as arrays ). PHP also supports two composite types: Array and object. These two types can be assigned to variables or returned from functions.

So far, PHP/FI 2 users should not feel any changes. However, when many other languages work hard, PHP promotes expression growth on the same path. PHP is an expression-oriented language. in this respect, almost everything is an expression. Consider the example we have already studied, "$ a = 5 ". We can easily see that there are two related values, an integer constant of 5, and the value of variable $ a is also updated to 5. But the fact is: there is only one relevant value, that is, the assigned value itself. The value assignment operation calculates the value to be allocated, that is, 5. In fact, it means "$ a = 5", and it doesn't have to worry about what it is, it is an expression with a value of 5. Therefore, some codes such as "$ B = ($ a = 5)" and "$ a = 5; $ B = 5" (semicolon indicates the end of the statement ). Because the order of value assignment operations is from right to left, you can also write "$ B = $ a = 5 ".

Another good example of object-oriented expressions is increasing and decreasing before and after. PHP/FI 2 and most users in other languages should be familiar with variables ++ and variables-symbols. That is, the increment and decrement operators. In PHP/FI 2, the statement "$ a ++" has no value (not an expression), so that you cannot assign values to it or use it in any other way. PHP changes it to an expression, similar to the C language, to enhance the incremental/progressive ability. In PHP and C, there are two types of increment: Increment before increment and increment after increment. in essence, both increment before and increment increases the value of the variable, the effects on variables are the same. The difference is the incremental expression value. Increment first, write "+ + $ variable", and calculate the added value (PHP adds the variable value before reading the value of the variable, so it is called "increment first "). Then increment, write '$ variable ++' to find the original value before the variable increments (PHP increases the value of the variable after reading the value of the variable, so it is called 'post incrementing '). Note: If the expression is incremented by + + $ a, the value of the expression is incremented by 1. if the expression is incremented by $ a ++, the value of the expression remains unchanged .]

A commonly used expression type is a comparison expression. These expressions evaluate 0 or 1, that is, FALSE or TRUE (respectively ). PHP Support> (greater than),> = (greater than or equal to), = (equal ),! = (Not equal to), <(less than), <= (less than or equal ). These expressions are commonly used in condition-based judgment statements, such as if statements.

Here, the last example we will study is the combined value assignment operator expression. You already know that if you want to add 1 to the variable $ a, you can simply write '$ a ++' or '++ $ '. But what if you want to add a value greater than 1 to a variable, such as 3? You can write '$ a ++' multiple times, but this is obviously not an efficient and comfortable method. a more common approach is '$ a = $ a + 3 '. '$ A + 3' calculates the value of $ a plus 3, and the obtained value is re-assigned to the variable $ a, so the value of $ a is increased by 3. In PHP and several other languages similar to C, you can complete the above functions in a more brief form, so it is clearer and faster. Add 3 to the current value of $ a, which can be written as follows: $ a + = 3. Here it means "get the value of variable $ a, add 3, and the result is allocated to variable $ a Again ". In addition to being simpler and clearer, it can also run faster. The value of '$ a + = 3' is like the value of a normal assignment operation, which is the value after the assignment. Note that it is not 3, but a combination of $ a and 3 (that is, the value allocated to $: what is the value of '$ a + = 3' here? Not 3, not the original value of $ a, but the value of $ a after the + 3 operation is completed ]. Any two operators can be used in the copy operator mode, for example, '$ a-= 5' (subtract 5 from the value of the variable $ ), '$ B * = 7' (variable $ B multiplied by 7), and so on.

Some expressions may be considered redundant if you have not seen them in other languages, such as the triple operators:

$first ? $second : $third

If the value of the first subexpression is TRUE (non-zero), the value of the second subexpression is calculated. The value is the value of the entire expression. Otherwise, it will be the value of the third subexpression.

In general, the following example can help you understand the increment and expression before and after:

<?phpfunction double($i){   return $i*2;}$b = $a = 5;        /* assign the value five into the variable $a and $b */$c = $a++;          /* post-increment, assign original value of $a                       (5) to $c */$e = $d = ++$b;    /* pre-increment, assign the incremented value of                       $b (6) to $d and $e *//* at this point, both $d and $e are equal to 6 */$f = double($d++);  /* assign twice the value of $d before                       the increment, 2*6 = 12 to $f */$g = double(++$e);  /* assign twice the value of $e after                       the increment, 2*7 = 14 to $g */$h = $g += 10;      /* first, $g is incremented by 10 and ends with the                       value of 24. the value of the assignment (24) is                       then assigned into $h, and $h ends with the value                       of 24 as well. */?>

At the beginning of this chapter, we have said that we will describe multiple types of statements, and as promised, expressions can be statements. However, not every expression is a statement. In this case, the form of a statement is 'expr'; ', and an expression ends with a semicolon. In '$ B = $ a = 5;', $ a = 5 is a valid expression, but it is not a statement. '$ B = $ a = 5;' is a valid statement.

The last thing worth mentioning is the true value of the expression. In many events, we mainly focus on conditional execution and loop. instead of focusing on the explicit values in the expression, we should pay attention to whether the value of the expression is TRUE or FALSE. Constants TRUE and FALSE (case-insensitive) are two possible Boolean values. If necessary, an expression is automatically converted to Boolean.

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 provide you with a good idea about what an expression is and how to build a useful expression.


The most important cornerstone of growth. In PHP, almost everything you write is an expression. The simplest but most accurate way to define an expression is "anything tha...

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.