Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 overSimplifiedTernaryTest($condition = false) {
    return ($condition) ? 'someString' : 'someOtherString';
}
like image 258
Stephen Avatar asked Dec 22 '22 23:12

Stephen


2 Answers

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.

like image 122
HoLyVieR Avatar answered Dec 29 '22 11:12

HoLyVieR


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;
like image 45
Svisstack Avatar answered Dec 29 '22 11:12

Svisstack