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';
}
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';
}
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;
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.
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.
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"; }