为什么可以';t使用内联if语句返回


Why can't you use an inline if statement to return?

我认为当您想要返回时,在PHP中使用内联if语句是有限制的。

function example($Variable)
{
    ( (int) (++$Variable) == 1 ) ? return true : return false;
}
$test = example(1);

预期:false

这给出了一个奇怪的错误,因为我以前从未见过。

错误类型:4

消息:语法错误,意外的"return"(T_return)

有人能解释为什么你不能在这样的if语句中使用return吗?

三进制已经返回结果。不能返回返回函数。

您可以在变量结果之前返回,如下所示:

function example($Variable)
{
    return ( (int) (++$Variable) == 1 ) ? true : false;
}
$test = example(1);