Share PHP Expression Learning manual _php tutorial

Source: Internet
Author: User
For

In PHP 3.0, almost everything you write is an expression. The simplest but most precise definition of an expression is "anything with a value".

A simple example is constants and variables.

When you write "$a = 5″, you give $a value ' 5′. (In this case, ' 5′ is an integer constant). In this case, you want to assign $a to a value of 5. So when you write $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 PHP expression is a function.

For example, consider the following function:

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 the return value. 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 three types of values: shaping values, floating-point values, and string values. PHP supports two mixed types (non-scalar): Arrays and objects. These two types of values can be assigned to variables or returned from functions.

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 shaping constant ' 5′, and the value of the variable that is also assigned to 5 $a. But there is actually an additional value, which is the value of the assignment statement itself.

The value of the assignment statement itself is the assigned value, in this case 5. In fact, it means not considering ' $a = what 5′ is going to do, it is an expression with a value of 5. In this way, write a statement 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 assignment is right-to-left you can also write ' $b = $a = 5′.

Another good example of the direction in which PHP expressions are calculated is the addition, the addition, the first minus, and then minus. Users of Php/fi and most other languages may be familiar with variable++ and variable–. This is a self-adding and self-subtraction operation. In Php/fi 2, the statement ' $a + + ' has no value (it is not an expression), so you can neither assign it to it nor use it in any way. PHP 3 turns them into the same expression as in C, which enhances the ability to add and subtract.

Similar to C, there are two types of self-added-plus and post-add in PHP 3. The essence of the first plus and the post-addition is the variable self-addition, the function of the variable itself is the same. The difference is the value of the self-added expression. Shape as ' + + $variable ' first add, calculate the value of the variable since the addition (PHP first do the variable to add, and then read its value, it is called ' first Plus '). After adding the shape of ' $variable + + ', the value of the original variable $variable is computed before the self-addition ( PHP does self-add after reading the value of the variable, so it is called ' post-add '.

The most common type of PHP expression is a comparison expression. This expression evaluates to 0 or 1, which means FALSE or TRUE, respectively.

PHP Support > (greater than), >= (greater than or equal), = = (equals), < (less than) and <= (less than equals). This expression is usually 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 add a $a, you can simply write the word ' $a + + ' or ' + + $a '. But what if the value to be increased is greater than 1, such as adding 3 to it? You can write a few more ' $a + + ', but this is obviously not an efficient or receptive approach.

Another common way is to write ' $a = $a + 3′. Calculate the value of ' $a + 3′ first, and return to the $a so that the $a adds 3. In PHP 3, you can abbreviate it like in several other languages (for example, C), so that it's clearer and easier to understand. The current variable $a plus 3 can be written as ' $a + = 3′. The meaning of this sentence is "take out the value of the $a, add it 3, in the $a".

This, in addition to making the statement brief and clear, also makes it execute faster. The PHP expression ' $a + = 3′ value, as well as a strict assignment statement, is the assigned value. Note: not 3, but the value of the $a plus 3 (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 value of the PHP expression. Many times (mostly during conditional execution and looping), you don't care about the specific value of an expression, but just notice that it represents true or FALSE (PHP does not have a dedicated Boolean type).

PHP uses a Perl-like method to calculate the true value of an expression. Any non-zero value is TRUE, and 0 is FALSE. Be sure to note that the negative zero value is a value of 0 and is considered TRUE! An 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.


http://www.bkjia.com/PHPjc/446023.html www.bkjia.com true http://www.bkjia.com/PHPjc/446023.html techarticle for PHP 3.0, almost everything you write is an expression. The simplest but most precise definition of an expression is anything that has a value. A simple example is constants and variables. ...

  • 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.