What are expressions in php?

怪我咯
Release: 2023-03-10 21:14:01
Original
2153 people have browsed it

Expression is 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 with a value".

The most basic expression forms are constants and variables. When typing "$a = 5", the value "5" is assigned to the variable $a. "5", obviously, has the value 5, in other words "5" is an expression with the value 5 (here, "5" is an integer constant).

After the assignment, the expectation is that the value of $a is 5, so if we write $b = $a, the expectation is that it will be the same as $b = 5. In other words, $a is an expression that also evaluates to 5. If everything works correctly, this is exactly what is going to happen.

An example of a slightly more complex expression is a function. For example, consider the following function:

<?php
function  foo  ()
{
    return  5 ;
}
?>
Copy after login

Assuming that you are already familiar with the concept of functions (if not, take a look at the chapter on functions), then typing $c = foo() is essentially the same as writing Next $c = 5, which is correct. Functions are also expressions, and the value of an expression is their return value. Since foo() returns 5, the expression "foo()" also evaluates to 5. Often functions don't just return a static value, but may compute something.

Of course, values ​​in PHP are often not integers. PHP supports four types of scalar values ​​(scalar values ​​cannot be split into smaller units, such as arrays): integer values ​​(integer), floating point values ​​(float), stringvalues ( string ) and boolean ( boolean ). PHP also supports two composite types: arrays and objects. Both types can be assigned to variables or returned from functions.

PHP develops along the expression path like other languages, but goes further. PHP is an expression-oriented language, in the sense that almost everything is an expression. Consider the example we just studied, "$a = 5". Obviously there are two values ​​involved here, the value of the integer constant 5 and the value of the variable $a, which is also updated to 5. But the fact is that there is an additional value involved, namely the value of the value-added statement itself. The assignment statement itself evaluates to the assigned value, which is 5. Effectively this means that "$a = 5", regardless of what it does, is an expression that evaluates to 5. Thus, writing " $b = ( $a = 5)" is the same as writing " $a =5; $b =5" (the semicolon marks the end of the statement). Because the order of assignment operations is from right to left, you can also write "$b = $a =5".

Another good example of expression-oriented is front and back increment and decrement. Users of PHP and most other languages ​​should be familiar with the 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 a value to it or use it in any other way. PHP enhances the increment/decrement capabilities by turning it into an expression, similar to the C language. In PHP and C language, there are two types of increment, pre-increment and post-increment. Essentially, both pre-increment and post-increment increase the value of the variable, and have the same impact on the variable. The difference is that the value of the expression is incremented. Pre-increment, written as "++$variable", finds the increased value (PHP increments the value of the variable before reading the value of the variable, so it is called "pre-increment"). Post-increment, written as "$variable++", finds the original value of the variable before it is incremented (PHP increments 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. These expressions evaluate to FALSE or TRUE . PHP supports > (greater than), >= (greater than or equal to), == (equal to), != (not equal to), < (less than), <= (less than or equal to). PHP also supports the equality operator === (same values ​​and types) and the non-identity operator !== (different values ​​or types). These expressions are most commonly used in conditional judgment statements, such as if statements.

这里,将要研究的最后一个例子是组合的运算赋值表达式。已经知道如果想要为变量 $a 加1,可以简单的写“$a++”或者“++$a”。但是如果想为变量增加大于 1 的值,比如 3,该怎么做?可以多次写“$a++”,但这样明显不是一种高效舒适的方法,一个更加通用的做法是“$a = $a + 3”。“$a + 3”计算 $a 加上 3 的值,并且得到的值重新赋予变量 $a,于是 $a 的值增加了3。在 PHP 及其它几种类似 C 的语言中,可以以一种更加简短的形式完成上述功能,因而也更加清楚快捷。为 $a 的当前值加 3,可以这样写:“$a += 3”。这里的意思是“取变量 $a 的值,加 3,得到的结果再次分配给变量 $a”。除了更简略和清楚外,也可以更快的运行。“$a += 3”的值,如同一个正常赋值操作的值,是赋值后的值。注意它不是 3,而是 $a 的值加上3 之后的值(此值将被赋给 $a)。任何二元运算符都可以用运算赋值模式,例如“$a -= 5”(从变量 $a 的值中减去 5),“$b *= 7”(变量 $b 乘以 7),等等。

还有一个表达式,如果没有在别的语言中看到过的话,可能看上去很奇怪,即三元条件运算符:

$first ? $second : $third

如果第一个子表达式的值是 TRUE (非零),那么计算第二个子表达式的值,其值即为整个表达式的值。否则,将是第三个子表达式的值。

下面的例子一般来说应该有助于理解前、后递增和表达式:

<?php
function  double ( $i )
{
    return  $i * 2 ;
}
$b  =  $a  =  5 ;         
$c  =  $a ++;           
$e  =  $d  = ++ $b ;      



$f  =  double ( $d ++);   
$g  =  double (++ $e );   
$h  =  $g  +=  10 ;       
?>
Copy after login

一些表达式可以被当成语句。这时,一条语句的形式是 'expr' ';',即一个表达式加一个分号结尾。在“$b=$a=5;”中,$a=5 是一个有效的表达式,但它本身不是一条语句。“$b=$a=5;”是一条有效的语句。

最后一件值得提起的事情就是表达式的真值。在许多事件中,大体上主要是在条件执行和循环中,不要专注于表达式中明确的值,反而要注意表达式的值是否是 TRUE 或者 FALSE 。常量 TRUE 和 FALSE (大小写无关)是两种可能的 Boolean 值。如果有必要,一个表达式将自动转换为 Boolean。参见类型强制转换一节。

PHP 提供了一套完整强大的表达式,而为它提供完整的文件资料已经超出了本手册的范围。上面的例子应该为你提供了一个好的关于什么是表达式和怎样构建一个有用的表达式的概念。在本手册的其余部分,我们将始终使用expr 来表示一个有效的 PHP 表达式。

The above is the detailed content of What are expressions in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!