Nested Ifs using ? : notation
by z3n on Mar.25, 2010, under Coding, Tips & Hints
Problem:
I can’t tell if this is a good practice, but i don’t really care, it speeds up the process.
I’m talking about this specific IF notation:
$x=1;
// this:
if ($x == 1) {
// do something
} else {
// do something
}
// .. can be replaced with this
return ($x == 1) ? /* true */ : /* false */;
// however... this:
if ($x == 1) {
// do something
} else {
if ($x == 2) {
// do something
} else {
// do something
}
}
// can't be replaced with the ? : notation
Solution:
function foo($x) { return $x; }
return ($x == 1) ? /* x = 1 */ : foo(($x == 2) ? /* x=2 */ : /* x = other */);
No comments for this entry yet...