PHP Basics-expression Introduction

Source: Internet
Author: User
Tags php basics php programming scalar

An expression is PHP The most important cornerstone. In PHP programming

, almost anything written is an expression. The simplest but most precise way to define an expression is "anything that has value."

The most basic form of expression is constants and variables. When you type "$a = 5", the value "5" is assigned to the variable $a. "5", it is obvious that its value is 5, in other words "5" is a value of 5 Expression ( in this case, "5" is an integer constant ).

after the assignment, the expected situation is $a the value is 5 , so if you write down $b = $a , the expectation is that it is like $b = 5 The same. In other words,$a is also an expression with 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:

function foo ()

{

return 5;

}

?>

Assuming you are already familiar with the concept of a function, type $c = foo () Essentially, it's like writing down $c = 5 , that's right. The function is also an expression, and the value of the expression is their return value. Since foo () returns 5, the value of theexpression "foo ()" is also 5. Normally a function does not simply return a static value, but it may calculate something.

of course, PHP the values in are often not integral types. PHP supports four scalar values ( scalar values cannot be split into smaller units, for example, and array ) types: integer values ( Integer), floating-point value (float), String value (String) , and Boolean value ( Boolean). PHP also supports two kinds of composite types: arrays and objects. Both types can be assigned to variables or returned from functions.

php  develops on the path of expression just like any other language, but advances more deeply. php  is an expression-oriented language, and in this respect almost everything is an expression. Consider the example that you have just studied, " $a = 5 ". It is clear that there are two values, the value of the integer constant " 5 " and the variable   $a   , which is also updated to  5 . But the fact is that there is an additional value, the value of the attached statement itself. The assignment statement itself evaluates to the assigned value, which is  5 . In fact, this means " $a = 5 ", which is a value of  5  , regardless of what it does. Thus, write " $b = ($a = 5) " and write " $a = 5; $b = 5 " ( semicolon marks the end of the statement ) is the same. Because the order of the assignment is 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. php  and users in most other languages should be more familiar with  variable++  and  variable--  The symbol. That is, the increment and decrement operators. In  php  , as in  c  , there are two types of increments--before and after increments, Essentially, both pre-increment and post-increment both increase the value of the variable, and the effect on the variable is the same, and the difference is the value of the increment expression. Increment, write " + + $variable " To add the value (php  adds the value of the variable before reading the value of the variable, so called "Before incrementing" ) . Increment, write " $variable + + " to find the original value before the variable is incremented (php  after reading the value of the variable, increase the value of the variable , which is called "post-increment" ) .

 

a common-to-expression type is a comparison expression. These expressions are evaluated as  false  or  true . php  support  > ( greater than ) , >= ( greater than or equal to ) , = = ( equals ) , ! = ( not equal to ) , < ( less than ) and  <= ( is less than or equal to ) . php  also supports the strict equality operator  === ( values and types are the same ) and non-congruent operators  !== ( value or different type ) . These expressions are commonly used in conditional judgment statements, such as if  statements.

here, the last example that will be studied is the combination of the assignment expression. Already know if you want to be a variable$aPlus1, you can simply write "$a + +"or"+ + $a". But if you want to increase the variable by more than1values, such as3, what to do?can write multiple times "$a + +", but this is obviously not an efficient and comfortable method, a more general practice is"$a = $a + 3". "$a + 3"equivalent to$aPlus3value, and the resulting value is re-assigned to the variable$a, so$aincreases the value of the3. In thePHPand several other languages such asC, this function can be done in a shorter form, and thus more clearly and quickly. To be$athe current value plus3, you can write this: "$a + = 3". This means "take a variable."$athe value, plus3, the resulting results are assigned again to the variable$a". In addition to being more brief and clear, it can be run faster. "$a + = 3Value, like the value of a normal assignment operation, is the value after the assignment. Note that it is not3, but$athe value plus3the value after(This value is assigned to the$a). Any binary operator can use an operation assignment pattern, such as "$a-= 5"(from Variable$aminus the value in the5), "$b *= 7"(variables$bMultiply7), and so on.

There is also an expression that, if not seen in another language, may seem strange, namely the ternary conditional operator:

$first? $second: $third

?>

if the value of the first sub-expression is TRUE ( not 0 ) , the second subexpression is evaluated, and its value is the value of the entire conditional expression. Otherwise, a third subexpression is evaluated and its value becomes the value of the entire expression.

The following example should generally help to understand pre-and post-increment and expression:

function double ($i)

{

return $i * *;

}

$b = $a = 5; /* Assign the value five intothe variable $a and $b */

$c = $a + +; /* post-increment, assignoriginal value of $a

(5) to $c */

$e = $d = + + $b; /* pre-increment, assignthe incremented value of

$b (6) to $d and $e */

/* At the point, both $d and $e is equalto 6 */

$f = Double ($d + +); /* Assign twice Thevalue of $d before

The increment, 2*6 = $f */

$g = double (+ + $e); /* Assign twice Thevalue of $e after

The increment, 2*7 = $g */

$h = $g + = 10; /* First, $g is Incrementedby and ends with the

Value of 24. The value of the assignment () is

Then assigned to $h, and $h ends with Thevalue

of as well. */

?>

Some expressions can be used as statements. At this point, the form of a statement is expr; , which is an expression with a semicolon ending. In ' $b = $a = 5; ' in which ' $a = 5 ' is a valid expression, but it is not a statement by itself. and ' $b = $a = 5; ' is a valid statement.

The last thing worth mentioning is the truth of the expression. In many cases, it is largely in terms of condition execution and looping that you do not care about the specific value in the expression, but only the value of the expression is TRUE or FALSE. The constant TRUE and FALSE ( case -insensitive ) are two possible Boolean values. When necessary, an expression is automatically converted to a Boolean value.



PHP Basics-expression Introduction

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.