Ternary Operators – php shorthand ?:

Ternary operators are probably the most confusing code to look at but at the same time probably the most underused style of coding for new developers. Ternary operators doesn’t give any additional functionality to PHP programming aside from simplifying and minimizing the number of characters you have you type in order to achieve the desired outcome. The goal with ternary operators is to make if/else statements fit on one line. I have found this to be very helpful if you need an if statement inside of an HTML element.

Here is a good scenario. You are making a form that edits some existing content. You want to pull in the content from your database and display in your form. For something like checkboxes you want to check if the value is “on”, output “checked” in your HTML, but if it is “off” you want to not display “checked”.

What this looks like without ternary operators:

That’s not too bad, but  with ternary operators we can simplify it even more.

Firstly, we are echoing the entire input and then we concatenate* our strings together. Just so you are aware, you want to wrap all of your HTML elements in a single quote ( ‘ ) and link it with your php with it using a dot ( . ).  

With this way of typing our if statement we removed the need to open and close PHP inside of the HTML element by echoing the entire thing. Then we simplify the if/else statement using ternary operators.

Ternary operators have the following syntax:

(condition) ? (return value) : (false return value)

You are essentially dropping the actual words “if/ else” and assuming that is what you mean by adding ? between your condition and your return TRUE value and : replaces the word else and returns if the condition is false.

Just one more example

 

Things you should know before using it

  • Ternary operators can get unruly fast! You really only want to use it to replace a simple if/else statement without too much logic built in.
  • Make sure to comment your code well so that you or a team member can go back and easily see what you were doing
  • If you find yourself using too many parenthesis, consider why you are using ternary operators in the first place. Is it really simplifying anything at that point?

con·cat·e·nate

kənˈkatnˌāt/

verb

formaltechnical
  1. link (things) together in a chain or series.
The_Idiot

About Lindsay

Self made millionaire.. Oops I mean web developer.. who started out learning how to build websites with tables and dreamweaver, I now almost entirely focus on CMS-based development, usually WordPress. I love coding and designing and regularly switch between my left and right brains.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">