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.
<?php
$a= "5";
echo $a;
?>
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 an expression with a value of 5. If everything runs correctly, this is the correct result that will happen.
Functions are also expressions, and the values of expressions are their return values. For example, consider the following functions:
<? PHP
Function fun ()
{
Return 5;
}
$ Shit = fun ();
Echo $ shit;
?>
Return Value: 5
Enter$ C = Foo ()In essence, it is like writing$ Shit = 5That's right. Since fun () returns 5, the value of the expression "Fun ()" 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)
Type: integer, float, string, and Boolean ). PHP also supports two composite types: array and object. These two types can be assigned to variables or returned from functions.
PHP, like other languages, has developed on the Expression path, but has made more strides. 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, the value of integer constant 5 and the value of 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 language users should be familiar with variables ++ and variables -- symbols. That is, the increment and decrement operators. In PHP/FI 2, the statement "$ A ++" has no value (not an expression), so that you cannot assign values to it or use it in any other way. PHP changes it to an expression, similar to the C language, to enhance the incremental/progressive ability. In PHP and C, there are two types of increment: Increment before increment and increment after increment. In essence, both increment before and increment increases the value of the variable, 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. Evaluate these expressionsFalseOrTrue. PHP support> (greater than),> = (greater than or equal to), = (equal ),! = (Not equal to), <(less than), <= (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 all in the condition judgment statement, for example,IfStatement.
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" calculates the value of $ A plus 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 similar to 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:
$first ? $second : $third
If the value of the first subexpression isTrue(Non-zero), then the value of the second subexpression is calculated, and its value is the value of the entire expression. Otherwise, it will be the value of the third subexpression.
The following example should generally help to understand the increment and expression before and after:
<?php
function double($i)
{
return $i*2;
}
$b = $a = 5; /* assign the value five into the variable $a and $b */
$c = $a++; /* post-increment, assign original value of $a
(5) to $c */
$e = $d = ++$b; /* pre-increment, 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 before
the increment, 2*6 = 12 to $f */
$g = double(++$e); /* assign twice the value of $e after
the increment, 2*7 = 14 to $g */
$h = $g += 10; /* first, $g is incremented by 10 and ends with the
value of 24. the value of the assignment (24) is
then assigned into $h, and $h ends with the value
of 24 as well. */
?>
Some expressions can be used as statements. In this case, a statement is in the form of '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 mainly focus on conditional execution and loop. Instead of focusing on the specific values in expressions, we should pay attention to whether the values of expressions areTrueOrFalse. ConstantTrueAndFalse(Case-insensitive) are two possible boolean values. If necessary, an expression is automatically converted to boolean. See force type conversion.
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 provide you with a good idea about what an expression is and how to build a useful expression. We will always use the rest of this ManualExprTo indicate a valid PHP expression.