PHP Shorthand IF Statements (Ternary Operator)

You know If statements. They are almost all the same in most of the programming languages. In PHP, their structure is also just the way you would expect them to be ­čÖé Below is a very simple if statement in PHP:

How about┬áPHP Shorthand IF Statements…?┬áSometimes you want them to be as short as possible, so that you can easily place┬áthem in your PHP code that emits some conditional HTML. That is where┬ácomes into play the shorthand version that uses ternary operator. Below is the short version of the above PHP if-else statement using ternary operator:

Using this ternary notation you could easily and very shortly create conditional, HTML echoing PHP code as below:

I find it very easy and very nice to use shorthand ternary PHP IF-ELSE statements. I recommend you use them for simpler/shorter/easier coding.

Hope this helps.
Good Luck,
Serdar

Leave a Reply

Your email address will not be published. Required fields are marked *