php if else with function quickhand

时间:2016-08-18 21:58:39

标签: php conditional-operator shorthand

我怎么能写这篇文章?

$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';

没有工作,请帮忙!

2 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:0)

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

我不想抑制错误,但可能:

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