Vocademy

PHP Tutorial

Ternary operators

A ternary operator is a somewhat unintuitive shorthand for some if-else operators. Here is a typical if-else example:

if(isset($Message)){$Text = $Message;}else{$Text = "";}

This is intuitive but can be done with the following ternary operator:

$Text = isset($Message) ? $Message : "";

The syntax begins with an opening partial statement, in this case, that is:

$Text =

The ternary follows and determines the rest of the statement. This example begins with the following conditional statement--isset($Message). If the variable $Message exists (is set), the outcome of the test is true. If so, the partial statement immediately following the question mark is appended to the opening partial statement. In this case, the complete opening statement becomes:

$Text = $Message;

If the ternary operator results in a false condition--in this example, the $Message variable doesn't exist--the partial statement immediately following the colon is appended to the opening partial statement. In this case, the complete opening statement becomes:

$Text = '';

Note that the obligatory semicolon is appended to complete the statement.

Type the following into your PHP editor (a slightly different way to use the operator).

<?
print isset($Message) ? "Message is $Message<br>" : "No message<br>";
$Message = "Hello World!";
print isset($Message) ? "Message is $Message<br>" : "No message<br>";
?>


Now save the code, name it ternary.php, upload it to your server, then go to your site and run the page.
If you typed everything correctly, you should see something like this:

No message
Hello World!

The first time the PHP processor encountered the ternary operator, there was no $Message variable set, so the resulting statement became:

print "No message";

The processor then printed the appropriate text. Then the $Message variable was set, and the second time the PHP processor encountered the ternary operator, the resulting statement became:

print "Message is $Message";

Then, the processor printed the appropriate text.

Here is another example of an if-else and its equivalent ternary operator

If-else:

if($Level => 50){print "Over level";}else{print "Under level";}

Ternary:

print ($Level => 50) ? "Over level" : "Under level";

The final statement becomes either:

print "Over level";

or

print "Under level";

Depending on the number contained in the $Level variable.

In some languages, the ternary operator can be used like a regular if statement by eliminating anything between the question mark and the colon. In PHP, if such a statement evaluates as true, it will return the number 1 (representing that the statement is true) instead of the expected nothing. Here is the above example modified as described:

print ($Level => 50) ?: "Under level";

This will print "Under level" if the $Level variable contains a number less than 50. However, if $Level contains a number equal to or greater than 50 it will print "1". You may or may not find this useful.

Vocademy