什么是?:在PHP 5.3中? [英] What is ?: in PHP 5.3?
问题描述
可能重复: 什么是PHP运算符?"和:"被调用,它们做什么?
<?PHP
require __DIR__.'/c.php';
if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; }))
throw new Exception('Error');
$c();
?>
Twitto使用了PHP 5.3中提供的几个新功能:
Twitto uses several new features available as of PHP 5.3:
- DIR 常量
- ?:运算符
- 匿名函数
- The DIR constant
- The ?: operator
- Anonymous functions
-
数字2与PHP 5.3中的?:有什么关系?
此外,匿名功能是什么意思?那不是已经存在了一段时间了吗?
Also, what do they mean by anonymous functions? Wasn't that something that has existed for a while?
推荐答案
?:
是条件运算符的一种形式,以前只能以以下方式使用:
?:
is a form of the conditional operator which was previously available only as:
expr ? val_if_true : val_if_false
在5.3中,可以省略中间部分,例如expr ?: val_if_false
等效于:
In 5.3 it's possible to leave out the middle part, e.g. expr ?: val_if_false
which is equivalent to:
expr ? expr : val_if_false
从手册:
自PHP 5.3起,可以省略条件运算符的中间部分.如果
expr1
计算为TRUE
,则表达式expr1 ?: expr3
返回expr1
,否则返回expr3
.
Since PHP 5.3, it is possible to leave out the middle part of the conditional operator. Expression
expr1 ?: expr3
returnsexpr1
ifexpr1
evaluates toTRUE
, andexpr3
otherwise.
这篇关于什么是?:在PHP 5.3中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!