From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Shorthand conditional expressions

Shorthand conditional expressions

- [David] Hi, I'm David Powers and welcome to this week's edition of PHP Tips, Tricks, and Techniques, designed to help you become a smarter, more productive PHP developer. This time, I want to take a look at shorthand conditional expressions. You're probably familiar with the ternary operator. It looks like this. It uses a simple conditional expression to assign a value to a variable. If the condition equates to true, the value after the question mark is used. But if it's false the value after the colon is used instead. That's the basic usage, but the ternary operator can also be used in a less familiar way. If you use a value that PHP treats as either true or false instead of a condition, you can then omit the expression between the question mark and the colon. If the value equates to true, it's assigned to the variable. Otherwise, the value after the colon is used. PHP 7 introduced another shorthand conditional operator called the null coalescing operator. Consisting of two…

Contents