Basic PHP expressions

Source: Internet
Author: User
Expressions are the most important cornerstone of PHP. Almost everything written in PHP is an expression. The simplest but most accurate way to define an expression is "anything with values ".

Expressions are the most important cornerstone of PHP. Almost everything written in PHP is an expression. The simplest but most accurate way to define an expression is "anything with values ".

The most basic expressions are constants and variables. When you type "$ a = 5", assign the value "5" to the variable $. "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 a value is assigned, we expect the value of $ a to be 5. Therefore, if $ B = $ a is written, we expect it to be like $ B = 5. In other words, $ a is also 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:

12345 Function foo () {return 5 ;}?>

If you are familiar with the concept of a function, typing $ c = foo () is essentially like writing $ c = 5. that's right. 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 four scalar values (scalar values cannot be split into smaller units, for example, different from arrays): integer, float, and string) and boolean ). PHP also supports two composite types: Array and object. These two types can be assigned to variables or returned from functions.

Like other languages, PHP has developed on the expression path, but it is more advanced.. PHP is an expression-oriented language. in this respect, almost everything is an expression. Consider the example that has been studied just now, "$ a = 5 ". Obviously, two values are involved here. The value of the integer constant "5" and the value of the variable $ a are also updated to 5. However, the fact is that an additional value is involved here, that is, the value of the value-attached statement itself. The value assignment statement evaluates to the assigned value, that is, 5. In fact, this means "$ a = 5". you don't have to worry about what it does. it is an expression with a value of 5. Therefore, write "$ B = ($ a = 5)" and "$ a = 5; $ B = 5" (the semicolon indicates the end of the statement) is the same. 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 and most other languages should be familiar with the variable ++ and variable symbols. That is, the increment and decrement operators. In PHP, like the C language, there are two types of increment: Increment first and increment later. in essence, both increment first and increment later increase the variable value, 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 "). "$ Variable ++" is written, and 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-increment ").

A commonly used expression type is a comparison expression. These expressions are evaluated as FALSE or TRUE. PHP Support> (greater than),> = (greater than or equal to), = (equal ),! = (Not equal to), <(less than) and <= (less than or equal ). PHP also supports full equal operator ===( both values and types are the same) and non-full equal operators! = (The value or type is different ). These expressions are commonly used in condition judgment statements, such as if statements.

Here, the last example to be studied is the combined assignment expression.If you want to add 1 to the variable $ a, you can simply write "$ a ++" or "++ $ ". But what should I do if I 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" is equivalent to the value added to $ a by 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 such as C, the above functions can be completed in a more brief form, so it is clearer and faster. Add 3 to the current value of $ a 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 the value after the value assignment, just like a normal value assignment operation. Note that it is not 3, but the value of $ a plus the value after 3 (this value will be assigned to $ ). Any binary operator can use the operation value assignment mode, for example, "$ a = 5" (subtract 5 from the value of the variable $ ), "$ B = 7" (variable $ B multiplied by 7) and so on.

Another expression, if not seen in other languages, may look strange, that is, the ternary conditional operator:

12 $ First? $ Second: $ third?>

If the value of the first subexpression is TRUE (non-zero), the second subexpression is evaluated, and its value is the value of the entire conditional expression. Otherwise, the third subexpression is evaluated and its value becomes the value of the entire expression.

The following example should generally help to understand the increment and expression before and after:

12345678910111213141516171819 Function double ($ I) {return $ i2;} $ B = $ a = 5; /assign the value five into the variable $ a and $ B/$ c = $ a ++;/postincrement, assign original value of $ a (5) to $ c/$ e = $ d = ++ $ B;/preincrement, 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 beforethe increment, 26 = 12 to $ f/$ g = double (++ $ e ); /Assign twice the value of $ e afterthe increment, 27 = 14 to $ g/$ h = $ g + = 10;/first, $ g is incremented by 10 and ends with thevalue of 24. the value of the assignment (24) isthen assigned into $ h, and $ h ends with the valueof 24 as well. /?>

Some expressions can be used as statements. In this case, the form of a statement is expr; that is, an expression is ended 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 generally focus on condition execution and loop, not on the specific values of expressions, but on whether the values of expressions are TRUE or FALSE. Constants TRUE and FALSE (case-insensitive) are two possible Boolean values. If necessary, an expression is automatically converted to a Boolean value.

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.