【问题标题】:php if else with function shorthandphp if else 函数简写
【发布时间】:2016-12-25 23:18:01
【问题描述】:

我怎么能这么快写出来?

$abc = file_get_contents('one.txt');
if($abc !== '')
{
msg($abc);
} else {
msg('nope');
}

我试过了:

$abc = file_get_contents('one.txt');
if($abc !== '') ? msg($abc) : msg('nope');

$abc = file_get_contents('one.txt');
msg if($abc !== '') ? $abc : 'nope';

不工作,请帮忙!

【问题讨论】:

    标签: php conditional-operator shorthand


    【解决方案1】:
    <?php
    msg(($abc = file_get_contents('blah.txt')) ? $abc : 'Nope');
    

    我不喜欢隐藏错误,但也许:

    msg(($abc = @file_get_contents('blah.txt')) ? $abc : 'Nope');
    

    【讨论】:

      【解决方案2】:

      在编写三元表达式时不要使用if 关键字。

      ($abc != '') ? msg($abc) : msg('nope');
      

      msg($abc != '' ? $abc : 'nope');
      

      【讨论】:

      • msg($abc ? $abc : 'Nope')?
      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 2017-01-14
      • 2014-09-22
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      相关资源
      最近更新 更多