Php inline if else if
Is there a way to have an inline if statement in PHP which also includes a elseif? Show
I would assume the logic would go something like this:
asked May 14, 2012 at 21:59
3
is identical to
Therefore, the ternary equivalent is
answered May 14, 2012 at 22:01
ridrid 58.6k30 gold badges145 silver badges189 bronze badges 1 you can use nested Ternary Operator
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
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,
In your case it will be:
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:
Tips for Using Ternary OperatorsHere are a few tips for when using "?:" logic:
More Sample UsageHere 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
Incredible Demos
How we use if ELSE and ELSE IF statement in PHP?if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more than two conditions. switch statement - selects one of many blocks of code to be executed.
Can I use if else in PHP?In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.
What is nested IF ELSE statement in PHP?The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.
What is Elseif?Alternatively referred to as elsif, else if is a conditional statement performed after an if statement that, if true, performs a function.
|