Purchased the book on Amazon.com or received a free copy at a user group or conference? Click here to create your account!

« Back to the blog

PHP, Web Forms and the Ternary Operator

Published on: October 1, 2009 @ 12:48:46 · 0 comments

If the great comedian Rodney Dangerfield were reincarnated as a programming contruct, he would undoubtedly return as the ternary operator. This is because the ternary operator, a powerful programming construct supported by most if not all of today's mainstream languages, just gets no respect, particularly from beginning programmers who sneer at its admittedly odd syntax. Yet to those programmers who look past its appearance, the ternary operator can be the life of the party time-and-again.

For those who aren't yet familiar with this syntactical gem, the ternary operator is an operator that accepts three operators. Written in pseudocode, its syntax looks like this:

$variable = condition ? if true : if false

Pretty strange, right? Let's look at a ternary operator as it would appear in actual PHP code:

$available = ($inventory > 0) ? "yes" : "no";

In this example, the variable $available will be assigned the string yes if $inventory is assigned a value greater than 0. Otherwise, $available will be assigned the string no. Of course, the same outcome could be had using an if-statement:

if ($inventory > 0) {
    $available = "yes";
} else {
    $available = "no";
}

As you can see, the ternary operator-based variation is far more succinct. This condensed form can be quite useful when HTML output is dependent upon some sort of dynamically changing situation, such as is the case when retrieving data from a database. For instance, I regularly use the ternary operator within the <select> tag when I need to pre-select a particular value in the list box. Here's an example:

01 <p>
02 Product:<br />
03 <select name="product-id">
04   <?php foreach($this->products AS $product) { ?>
05  <option value="<?= $product->id ?>" 
06  <?= $this->discount->product_id == $product->id ? "selected" : ""; ?>>
07  <?= $product->title; ?>
08  </option>
09   <?php } ?>
10 </select>
11 </p>

In this example, the ternary operator comes into play on line 06, where we're comparing the $this->discount object's product_id attribute value with that found in the $product object's id attribute. If equal, selected will be output, causing that particular option to be preset.

Given such capabilities, the next time you come across the ternary operator, raise a glass and give it some respect!

Comments

Nobody has commented on this blog post yet. Why don't you be the first?

Add a Comment

Only registered users can ask questions and post comments. If you're already registered login now. Don't have an account? Registering only takes a moment and it's free! Register now.

 Join 700+ subscribers!


Have a question? Contact us!

The Tag Cloud

The Community Firehose

News submitted by and about the PHP and Web development community.

Creating a Weather Forecast with PHP and the Google Weather API
Submitted by wjgilmore

How to Create an Image Gallery Powered By PHP and Picasa
Submitted by wjgilmore

Easy Databasing with SQLite
Submitted by wjgilmore

PHP IDE Roundup
Submitted by wjgilmore

Five Cool PHP Array Functions
Submitted by wjgilmore

Use jQuery with Greasemonkey
Submitted by wjgilmore

Encapsulation in PHP
Submitted by wjgilmore

Talking to Google Charts with PHP
Submitted by wjgilmore

Talking to Google Analytics with PHP
Submitted by wjgilmore

Building a jQuery/PHP-powered Chat Room
Submitted by wjgilmore

Cloud Computing with PHP
Submitted by wjgilmore

BattleMaster: A Web-based game written in PHP
Submitted by wjgilmore

Create an Image Rotator with CSS and jQuery
Submitted by wjgilmore

Submit News | News Archive

Copyright © 2009 W.J. Gilmore, LLC. All Rights Reserved. Purchase Policy · Privacy Policy
Trademarked names may appear on this website. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to no benefit of the trademark owner, with no intention of infringement of the trademark.