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 */
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |