I hate it when I am happy about something "geeky," but this is one of those times. I was unhappy with the way I was writing a simple little dropdown box to display/update status. Since there are only 3 options, this box was originally hardcoded and was being outputted with echo. This method is far too static and is a true pain in the ass for selecting the current status. The code would look something like this:
echo "
if($order_status == 0){
echo " selected ";
}
echo "value=\"0\">New";
Of course you have to repeat the option 2 more times and set the if statement accordingly. Here comes Ternary to save the day. By creating an array with the statuses:
$statuses = array("New", "Shipped", "Failed");
I now loop through the array and use the Ternary Operator to assign the variable. The new code looks like this:
$selected = ($order_status == $stat) ? " selected " : "";
echo "$stat";
The Ternary took 15 lines of code down to 2. Talk about optimizing code. I encourage you to use Case Selects (Switch) and Ternary as often as possible. Leave the ifs at home for the sake of making your code legible, in case I ever have to read it.