views:

77

answers:

5

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';
}
+2  A: 

Yes, you can do that.

Example

Rocket
+1  A: 

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;
Svisstack
A: 

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.

Rob
+1  A: 

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.

HoLyVieR
Actually TheMagician suggested codepad.org, and both of these are great. Thanks!
Stephen
@Stephen You can also look at the link I just added for other online tool to test your code.
HoLyVieR
A: 

In php 5.3 you should use lambda function, see more in: http://php.net/manual/en/functions.anonymous.php

Sample:

$test = function($a) { return $a === true ? "x" : "y"; }
Felipe Cardoso Martins
Why is this better than a ternary?
Stephen
How is this even related to the question?
Michael Mrozek