开发者

Can I use a ternary in a PHP class method return statement?

Can I do this? (I can't test it at the moment to see for myself)

public function overSimplifiedTe开发者_开发百科rnaryTest($condition = false) {
    return ($condition) ? 'someString' : 'someOtherString';
}


It works and next time you can use ideone.com to test your code instead of asking question.

Your code : http://ideone.com/2oHkF

You can also refer to this question for additionnal online tool to test your code.


This is working.

Example:

class CClass
{
    public function overSimplifiedTernaryTest($condition = false)
    {
        return ($condition) ? 'someString' : 'someOtherString';
    }
}

$x = new CClass();
echo $x->overSimplifiedTernaryTest(false) . 
     '\r\n' . 
     $x->overSimplifiedTernaryTest(true);
delete $x;


Yes, you can do that.

Example


The ternary operator will return one value, which is what you then return from the function. This is why what you're trying to do will be no problem. It'll also work in other languages, you could do this in Javascript also for example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜