Share the PHP expression learning Manual

Source: Internet
Author: User

For

In PHP 3.0, almost everything 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 $ ′. (In this case, '5' is an integer constant ). Here, you want to assign $ a to 5. So when you write $ B = $ a, the expected result is $ B = 5. $ A is an expression with a value of 5.

A simple example of a complex PHP expression is a function.

For example, consider the following functions:

Function foo () {return 5 ;}

If you think that writing $ c = foo () is actually the same as writing $ c = 5, then you are right. A function is an expression whose value is returned. Because foo () returns 5, the value of 'foo () 'is 5.

PHP values are not limited to integer values, and are generally not. PHP supports three types of values: integer value, floating point value, and string value. PHP supports two types of mixing (non-scalar): array and object. These two types of values can be assigned to variables or returned from functions.

PHP 3 is a language for PHP expressions, so almost everything is expressions.

Consider the example we have discussed, '$ a = 5 ′. It is easy to see that there are two values, the value of the integer constant '5', and the value of the variable $ a which is also assigned as 5. However, there is actually an additional value, that is, the value of the value assignment statement itself.

The value of the value assignment statement is the value assigned. In this example, the value is 5. in fact, it means not to consider what '$ a = 5' is to do. It is an expression with a value of 5. In this way, write a statement such as '$ B = ($ a = 5)', like '$ a = 5; $ B = 5; '(there is a semicolon at the end of each statement ). Because the assignment order is from right to left, you can also write '$ B = $ a = 5 ′.

Another good example of the PHP expression direction is to add, add, subtract, And then subtract first. PHP/FI and users in most other languages may be familiar with variable ++ and variable -. This is an auto-increment and auto-increment operation. In PHP/FI 2, the '$ a ++' statement has no value (it is not an expression), so you can neither assign a value to it nor use it in any way. PHP 3 converts them into the same expressions as C, thus enhancing the self-addition and auto-subtraction operations.

Similar to C, PHP 3 also has two types of auto-addition: First addition and then addition. In essence, the first addition and the second addition are variable auto-addition, which have the same effect on the variable itself. The difference is the value of the Self-added expression. Like '+ + $ variable', add the variable first, and calculate the value of the variable after auto-addition (PHP first performs variable auto-addition and then reads its value, which is also called 'add First '). for example, after adding '$ variable + +', the original variable $ variable value is calculated before auto-addition (PHP then performs auto-addition after reading the variable value, so it is called 'postadd ').

The most common PHP expression is a comparison expression. The calculated result of this expression is 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 ). This expression is usually used in conditional execution, such as the IF statement.

The expression we will discuss at the end is a mixed value assignment expression. You already know that to add $ a plus one, you can simply say '$ a ++' or '+ + $ '. But what if the value to be added is greater than 1, for example, adding 3 to it? You can write '$ a ++' multiple times, but this is obviously not an efficient or acceptable method.

Another common method is to write '$ a = $ a + 3 ′. Calculate the value '$ a + 3' and return it to $ a. Then, 3 is added to $. In PHP 3, you can simply name it like in several other languages (such as C), so it is clearer and easier to understand. You can write the current variable $ a plus 3 as '$ a + = 3 ′. The meaning of this sentence is "get the value of $ a, add it to 3, and assign it to $ ".

This not only makes the statement short and clear, but also makes it run faster. The value of the PHP expression '$ a + = 3' is assigned, just like a strict value assignment statement. Note: instead of 3, it is the value of $ a plus 3 (this is assigned to $ ). Any double operators can be used in this assignment operation mode, for example, '$ a-= 5' (variable $ a minus 5 ), '$ B * = 7' (variable $ B multiplied by 7), and so on.

It is worth mentioning the true value of the PHP expression. Many times (mainly during conditional execution and loop), you don't care about the specific value of the expression, but pay attention to whether it represents TRUE or FALSE (PHP does not have a dedicated boolean type ).

In PHP, the perl-like method is used to calculate the true value of an expression. Any non-zero value is TRUE, and zero is FALSE. Please note that the negative zero value is a non-zero value and is considered TRUE! The value of "0" is FALSE for a null string, and that of all other strings is TRUE. For non-quantity values (arrays and objects)-if its value does not contain any element, it is FALSE; otherwise, it is TRUE.


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.