
PHP INLINE IOFF PORTABLE
This way if you want your code to be decently portable between languages, if that's a concern. Which is also usable in PHP in the same way. For all other cases adapt your code accordingly. Though be pleasantly surprised is they are. So you should never expect the same behavior in any of them. Not all languages do things the same way. To learn more about ternary operators and usage, visit PHP.net Comparison Operators.

$message = 'Hello '.($user->get('first_name') ?: 'Guest') /* echo, inline */Įcho 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody') //harsh! /* a bit tougher */Įcho '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 */ $message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest') /* shorthand usage */ Here are a couple more uses of ternary operators, ranging from simple to advanced: /* another basic usage */

PHP's behaviour when using more than one ternary operator within a single statement is non-obvious." "Is is recommended that you avoid "stacking" ternary expressions.

You can do your if/else logic inline with output instead of breaking your output building for if/else statements.Makes coding simple if/else logic quicker.There are some valuable advantages to using this type of logic: $var_is_greater_than_two = ($var > 2 ? true : false) // returns true What Are The Advantages of Ternary Logic? Ternary operator logic is the process of using "(condition) ? (true return value) : (false return value)" statements to shorten your if/else structures. If/Else statements aren't optimal (or necessary) in all situations. 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 are great but they can be too long. If / Else statements are easy to code and global to all languages. An essential part of programming is evaluating conditions using if/else and switch/case statements.
