什么是?:在PHP 5.3中? [英] What is ?: in PHP 5.3?

查看:120
本文介绍了什么是?:在PHP 5.3中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复: 什么是PHP运算符?"和:"被调用,它们做什么?

来自 http://twitto.org/

<?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:

  1. DIR 常量
  2. ?:运算符
  3. 匿名函数
  1. The DIR constant
  2. The ?: operator
  3. Anonymous functions


  1. 数字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 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

这篇关于什么是?:在PHP 5.3中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆