Is there a way to have an inline if statement in PHP which also includes a elseif?
I would assume the logic would go something like this:
$unparsedCalculation = [$calculation > 0] ? "".$calculation : [$calculation < 0] ? "".$calculation : $calculation;
asked May 14, 2012 at 21:59
3
elseif
is nothing more than else if
, so, practically, there is no elseif
, it's just a convenience. The same convenience is not provided for the ternary operator, because the ternary operator is meant to be used for very short logic.
if [$a] { ... } elseif [$b] { ... } else { ... }
is identical to
if [$a] { ... } else { if [$b] { ... } else { ... } }
Therefore, the ternary equivalent is
$a ? [ ... ] : [ $b ? [ ... ] : [ ... ] ]
answered May 14, 2012 at 22:01
ridrid
58.6k30 gold badges145 silver badges189 bronze badges
1
you can use nested Ternary Operator
[IF ? THEN : ELSE]
[IF ? THEN : ELSE[IF ? THEN : ELSE[IF ? THEN : ELSE]]
for better readability coding standard can be found here
answered May 14, 2012 at 22:04
You need to wrap some of that in parenthesis for order of operation issues, but sure, you could do that. While there is no "elseif" for ternary operators, it's effectively the same thing
if [condition] ? [true] : [false> if [condition] ? [true] : [false]];
Though you really shouldn't code like this...it's confusing from a readability perspective. Nobody is going to look at that and be "sweet, ninja!" they will say "ugh, wtf"
answered May 14, 2012 at 22:04
Crayon ViolentCrayon Violent
31.6k5 gold badges53 silver badges78 bronze badges
Try this,
[[$condition_1] ? "output_1" : [[$condition_2] ? "output_2" : "output_3"]];
In your case it will be:
$unparsedCalculation = [[$calculation > 0] ? "".$calculation : [[$calculation < 0] ? "".$calculation : $calculation]];
answered Apr 20, 2018 at 13:57
B.KB.K
81710 silver badges6 bronze badges
An essential part of programming is evaluating conditions using if/else and switch/case statements. If / Else statements are easy to code and global to all languages. If / Else statements are great but they can be too long.
I preach a lot about using shorthand CSS and using MooTools to make JavaScript relatively shorthand, so I look towards PHP to do the same. If/Else statements aren't optimal [or necessary] in all situations. Enter ternary operators.
Ternary operator logic is the process of using "[condition] ? [true return value] : [false return value]" statements to shorten your if/else structures.
What Does Ternary Logic Look Like?
/* most basic usage */ $var = 5; $var_is_greater_than_two = [$var > 2 ? true : false]; // returns true
What Are The Advantages of Ternary Logic?
There are some valuable advantages to using this type of logic:
- Makes coding simple if/else logic quicker
- You can do your if/else logic inline with output instead of breaking your output building for if/else statements
- Makes code shorter
- Makes maintaining code quicker, easier
- Job security?
Tips for Using Ternary Operators
Here are a few tips for when using "?:" logic:
- Don't go more levels deep than what you feel comfortable with maintaining.
- If you work in a team setting, make sure the other programmers understand the code.
- PHP.net recommends avoiding stacking ternary operators. "Is [sic] is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious."
- If you aren't experienced with using ternary operators, write your code using if/else first, then translate the code into ?'s and :'s.
- Use enough parenthesis to keep your code organized, but not so many that you create "code soup."
More Sample Usage
Here are a couple more uses of ternary operators, ranging from simple to advanced:
/* another basic usage */ $message = 'Hello '.[$user->is_logged_in[] ? $user->get['first_name'] : 'Guest'];
/* shorthand usage */ $message = 'Hello '.[$user->get['first_name'] ?: 'Guest'];
/* echo, inline */ echo 'Based on your score, you are a ',[$score > 10 ? 'genius' : 'nobody']; //harsh!
/* a bit tougher */ $score = 10; $age = 20; echo 'Taking into account your age and score, you are: ',[$age > 10 ? [$score < 80 ? 'behind' : 'above average'] : [$score < 50 ? 'behind' : 'above average']]; // returns 'You are behind'
/* "thankfully-you-don't-need-to-maintain-this" level */ $days = [$month == 2 ? [$year % 4 ? 28 : [$year % 100 ? 29 : [$year %400 ? 28 : 29]]] : [[$month - 1] % 7 % 2 ? 30 : 31]]; //returns days in the given month
To learn more about ternary operators and usage, visit PHP.net Comparison Operators.
Recent Features
An Interview with Eric Meyer
Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...
5 HTML5 APIs You Didn’t Know Existed
When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It." Can you blame us though? We watched the fundamental APIs stagnate for so long that a basic feature...
Incredible Demos
CSS Counters
Counters. They were a staple of the Geocities / early web scene that many of us "older" developers grew up with; a feature then, the butt of web jokes now. CSS has implemented its own type of counter, one more sane and straight-forward than the ole...