PHP语法问题:问号和冒号是什么意思?(复制)

时间:2021-02-26 22:24:34

Possible Duplicate:
quick php syntax question

可能重复:快速php语法问题

return $add_review ? FALSE : $arg;

What do question mark and colon mean?

问号和冒号是什么意思?

Thanks

谢谢

2 个解决方案

#1


118  

This is the PHP ternary operator (also known as a conditional operator) - if first operand evaluates true, evaluate as second operand, else evaluate as third operand.

这是PHP三元运算符(也称为条件运算符)——如果第一个操作数计算为true,则计算为第二个操作数,否则计算为第三个操作数。

Think of it as an "if" statement you can use in expressions. Can be very useful in making concise assignments that depend on some condition, e.g.

把它想象成可以在表达式中使用的“if”语句。可以非常有用的做简洁的作业,取决于一些条件,例如。

$param = isset($_GET['param']) ? $_GET['param'] : 'default';

There's also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise. For example:

还有一个简写版本(在PHP 5.3中)。可以省略中间操作数。如果第一个操作数为真,运算符将作为第一个操作数进行计算,而第三个操作数为假。例如:

$result = $x ?: 'default';

It is worth mentioning that the above code when using i.e. $_GET or $_POST variable will throw undefined index notice and to prevent that we need to use a longer version, with isset or a null coalescing operator which is introduced in PHP7:

值得一提的是,上面的代码在使用$_GET或$_POST变量时,会抛出未定义的索引通知,为了防止使用更长的版本,我们需要使用isset或PHP7中引入的空合并操作符:

$param = $_GET['param'] ?? 'default';

#2


14  

It's the ternary form of the if-else operator. The above statement basically reads like this:

它是if-else算子的三元形式。上面的语句基本上是这样的:

if ($add_review) then {
    return FALSE; //$add_review evaluated as True
} else {
    return $arg //$add_review evaluated as False
}

See here for more details on ternary op in PHP: http://www.addedbytes.com/php/ternary-conditionals/

有关PHP中的三元op的详细信息,请参见这里:http://www.addedbytes.com/php/ternary-conditionals/

#1


118  

This is the PHP ternary operator (also known as a conditional operator) - if first operand evaluates true, evaluate as second operand, else evaluate as third operand.

这是PHP三元运算符(也称为条件运算符)——如果第一个操作数计算为true,则计算为第二个操作数,否则计算为第三个操作数。

Think of it as an "if" statement you can use in expressions. Can be very useful in making concise assignments that depend on some condition, e.g.

把它想象成可以在表达式中使用的“if”语句。可以非常有用的做简洁的作业,取决于一些条件,例如。

$param = isset($_GET['param']) ? $_GET['param'] : 'default';

There's also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise. For example:

还有一个简写版本(在PHP 5.3中)。可以省略中间操作数。如果第一个操作数为真,运算符将作为第一个操作数进行计算,而第三个操作数为假。例如:

$result = $x ?: 'default';

It is worth mentioning that the above code when using i.e. $_GET or $_POST variable will throw undefined index notice and to prevent that we need to use a longer version, with isset or a null coalescing operator which is introduced in PHP7:

值得一提的是,上面的代码在使用$_GET或$_POST变量时,会抛出未定义的索引通知,为了防止使用更长的版本,我们需要使用isset或PHP7中引入的空合并操作符:

$param = $_GET['param'] ?? 'default';

#2


14  

It's the ternary form of the if-else operator. The above statement basically reads like this:

它是if-else算子的三元形式。上面的语句基本上是这样的:

if ($add_review) then {
    return FALSE; //$add_review evaluated as True
} else {
    return $arg //$add_review evaluated as False
}

See here for more details on ternary op in PHP: http://www.addedbytes.com/php/ternary-conditionals/

有关PHP中的三元op的详细信息,请参见这里:http://www.addedbytes.com/php/ternary-conditionals/