PHP learning PHP expression _php base

Source: Internet
Author: User
PHP expressions

An expression is the most important component of PHP. 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 a value of ' 5 ' to $a. (In this case, ' 5 ' is an integer constant). In this case, you want to assign the $a to a value of 5. So when writing $b = $a, the result you want is $b = 5.   That is, $a is an expression with a value of 5. A simple example of a complex 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 by its values. Because Foo () returns 5, the value of the expression ' foo () ' is 5.
The value of PHP is certainly not limited to shaping, and is usually not. PHP supports types of three-class values: Cosmetic values, floating-point values, and string values. PHP supports two types of blends (Non-scalar): Arrays and objects. These two types of values can be assigned to a variable or returned from a function.
PHP 3 is an expression-oriented language, so almost everything is an expression.
Consider the example we have discussed, ' $a = 5 '. It is easy to see that there are two values, the value of the shape constant ' 5 ', and the variable that is also assigned to 5 $a. But there is actually an additional value here, which is the value of the assignment statement itself.
The value of the assignment statement itself is the value that is assigned, in this case 5. In fact, it means not thinking about what ' $a = 5 ' To do, it's an expression with a value of 5. In this way, write statements such as ' $b = ($a = 5) ', just like ' $a = 5; $b = 5; ' (There is a semicolon at the end of each statement). Because the order of the assignment is Right-to-left you can also write ' $b = $a = 5 '.

Another good example of the direction of the expression calculation is to add first, then add and subtract first, and then subtract. Users of Php/fi and most other languages may be familiar with variable++ and variable--。 This is a self adding and a self subtraction operation. In Php/fi 2, the statement ' $a + + ' has no value (it is not an expression) so that you cannot assign to it or use it in any way. PHP 3 makes them the same expression as C, which enhances the ability to add and subtract from the operation.
Similar to C, PHP 3 also has two types of self-add----first plus and then plus. The essence of the addition and the addition is the variable, and the function of the variable itself is the same. The different points are the values of the self-expression. In the form of "+ + $variable," The first addition, the value of the calculated variable (PHP first to do the variable, and then read its value, also known as the ' first Plus '). In addition to the "$variable + +", the value of the original variable $variable is calculated before it is added ( PHP reads the value of the variable and then adds it, so it's called ' back-add '.
The most common expression is the comparison expression. This expression evaluates to 0 or 1, respectively, meaning FALSE or TRUE.
PHP Support > (greater than), >= (greater than equals), = = (equals), < (less than) and <= (less than equals). This expression is typically used in conditional execution, such as an IF statement.
The last expression we want to discuss here is a mixed assignment expression. You already know that if you want to make $a add one, you can simply write a ' $a + + ' or ' + + $a '. But what if you want to add a value that is bigger than 1, for example, to make it add 3? You can write ' $a + + ' more than once, but this is clearly not an efficient or acceptable approach. Another common method is to write ' $a = $a + 3 '. The value of ' $a + 3 ' is calculated first, and then the $a is added to the $a, which adds 3. In PHP 3, you can abbreviate it just as you would in several other languages (such as C), so that it is clearer and faster to understand. The current variable $a plus 3 can be written as ' $a + = 3 '. Take out the value of $a, add 3 to it, and assign it to $a. This, in addition to making the statement short and clear, also makes it perform faster. The value of the expression ' $a + = 3 ', like a strict assignment statement, is the assigned value. Note: not 3, but $a plus 3 value (which is assigned to $a). Any double operator can be used for this assignment operation mode, such as ' $a-= 5 ' (variable $a minus 5), ' $b *= 7 ' (Variable $b multiplied by 7), and so on.
Finally, it is worth mentioning the truth of the expression. Most of the time (mainly in the condition of execution and loop), you don't care about the specific value of the expression, but just notice whether it represents true or FALSE (PHP does not have a dedicated Boolean type). PHP uses a Perl-like approach to evaluate the truth of an expression. Any nonzero value is TRUE, and 0 is FALSE. Be sure to note that the value of minus zero is a 0 value and is considered TRUE! The empty string can be a string "0" to FALSE; The other string is TRUE. For non-quantity values (arrays and objects)-TRUE if its value does not contain any elements that are FALSE.

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.