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 中提供了几个新功能:
- DIR 常量
- ?: 运算
- 符 匿名函数
PHP 5.3 中的 number 2 与 ?: 有何作用?
另外,匿名函数是什么意思?这不是已经存在了一段时间了吗?
Possible Duplicate:
What are the PHP operators “?” and “:” called and what do they do?
From http://twitto.org/
<?PHP
require __DIR__.'/c.php';
if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; }))
throw new Exception('Error');
$c();
?>
Twitto uses several new features available as of PHP 5.3:
- The DIR constant
- The ?: operator
- Anonymous functions
What does number 2 do with the ?: in PHP 5.3?
Also, what do they mean by anonymous functions? Wasn't that something that has existed for a while?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
?:
是条件运算符的一种形式,以前仅可用作:在 5.3 中,可以省略中间部分,例如
expr ?: val_if_false
相当于:来自手册:
?:
is a form of the conditional operator which was previously available only as:In 5.3 it's possible to leave out the middle part, e.g.
expr ?: val_if_false
which is equivalent to:From the manual:
?:
运算符是 条件运算符(通常称为三元运算符):在以下情况下:
如果
expr1
为 true,则表达式计算为expr1
的值,否则为expr2
:The
?:
operator is the conditional operator (often refered to as the ternary operator):In the case of:
The expression evaluates to the value of
expr1
ifexpr1
is true andexpr2
otherwise:看这里:
匿名函数:不,它们在 5.3 之前不存在。 0(参见示例下面的第一个注释),至少以这种方式:
唯一的方法是
create_function()
,它速度较慢,相当麻烦且容易出错(因为使用字符串用于函数定义)。Look here:
Anonymous functions: No, they didn't exist before 5.3.0 (see the first note below the examples), at least in this way:
The only way was
create_function()
, which is slower, quite cumbersome and error prone (because of using strings for function definitions).