_php tutorial for Expressions in PHP

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

The most basic form of expression is constants and variables. When you type "$a = 5", the value ' 5 ' will be assigned to the variable $a. ' 5 ', obviously, a value of 5, in other words ' 5 ' is an expression with a value of 5 (So, ' 5 ' is an integer constant).

After the assignment, the expectation is that the $a value is 5, so if you write down $b = $a, 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:

     

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, and you are right. The function is also an expression, and the value of the expression is their return value. Since Foo () returns 5, the value of 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 three types of scalar values: integer values, floating-point values, and string values (scalar values cannot be split into smaller units, such as arrays). PHP also supports two kinds of composite types: arrays and objects. Both types can be assigned to variables or returned from functions.

So far, Php/fi 2 of users should not feel any change. However, when many other languages are working on it, PHP promotes expression growth on the same path. PHP is an expression-oriented language, and in this respect almost everything is an expression. Consider the example we have just studied, "$a = 5". It is easy to see that there are two related values, integer constant 5, and the value of the variable $a is updated to 5. But the fact is that there is only one relevant added value, that is, the assigned values themselves. The assignment operation calculates the value to be assigned, which is 5. In fact, it means "$a = 5", not necessarily what it does, but an expression with a value of 5. Thus, some code like this "$b = ($a = 5)" and "$a = 5; $b = 5 "(Semicolon marks the end of the statement). Because the order of 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/fi 2 and most other languages should be more familiar with variable + + and variable--symbols. That is, 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"). Post-increment, write ' $variable + + ', to find the original value before the variable is incremented (PHP reads the value of the variable, increases the value of the variable, and is therefore called ' post increment '). "Translator Note: before increment, + + $a, the value of the expression is incremented by 1, followed by increment, $a + +, the value of the expression does not change. 】

A common-to-expression type is a comparison expression. These expressions are evaluated with a value of 0 or 1, which is FALSE or TRUE (respectively). PHP Support > (greater than), >= (greater than or equal), = = (equals),! = (not equal to),< (less than), <= (less than equals). These expressions are most commonly used in conditional judgment statements, such as the IF statement.

Here, the last example we will look at is a combination of assignment operator expressions. You 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, like 3? You can write ' $a + + ' multiple times, but this is obviously not an efficient and comfortable method, a more general practice is ' $a = $a + 3 '. ' $a + 3 ' calculates $a plus 3 value, and the resulting value is re-assigned to the variable $a, so the $a value increases by 3. In PHP and several other C-like languages, you can do this in a shorter form, and it's more clear and fast. Add 3 to the current value of the $a, which you can write: $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 of ' $a + = 3 ', like the value of a normal assignment operation, is the value after the assignment. Note that it is not 3, but $a plus 3 of the combined value (i.e. the value already assigned to $a) "Translator Note: What is the value of the expression ' $a + = 3 '? Not 3, not the original value of the $a, but the value of the variable $a after the +3 operation has been completed. " Any two-bit operator can be used in the copy operator mode, such as ' $a-= 5 ' (subtract 5 from the value $a the variable), ' $b *= 7 ' (Variable $b multiplied by 7), and so on.

There are expressions that, if you have not seen in other languages, may be considered superfluous, such as triple operators:

$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 be able to help you understand the pre-and post-increment and expression slightly:

     

At the beginning of this chapter, we have said that we will describe a variety of statement types and, as promised, expressions can be statements. However, not every expression is a statement. In this case, a statement, which is in the form of ' expr '; ', an expression has a semicolon-ending. 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 an 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.

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.


http://www.bkjia.com/PHPjc/446736.html www.bkjia.com true http://www.bkjia.com/PHPjc/446736.html techarticle expressions are the most important cornerstone of PHP. In PHP, almost everything you write is an expression. The simple but most precise 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.