Professional Documents
Culture Documents
Use of Ternary Operator in PHP Fahmida Yesmin Marielle Sarong
Use of Ternary Operator in PHP Fahmida Yesmin Marielle Sarong
Excerpt: ternary operator is one of the shorthand comparison operators in PHP that’s used as an
alternative to any simple if-else statement. It has three operands: conditional statement, a
statement for true condition, and a statement for the false condition. This article explained the
different various uses of the ternary operator.
Permalink: use-ternary-operator-in-php
Category: php
If-else statements are normally used to define conditional statements in any programming
language. ternary operator(?:) can be used as the alternative of any simple if-else statement. It
is one of the shorthand comparison operators in PHP and contains three operands: the conditional
statement, the statement for true condition, and the statement for the false condition. This
operator is better implemented with a simple logical statement with a short code because it is
better to maintain and can be defined in a single statement. The uses of this operator are
explained in this tutorial.
Syntax:
The syntax of the ternary operator is given below:
conditional statement ? statement-1 : statement-2
'?' and ':' symbols are used to define the ternary statement. The first part of this statement
contains the conditional statement. If the conditional statement returns true, statement-1 will be
executed, otherwise, the statement-2 will be executed.
Output:
The following output will appear after running the script from the server. According to the script,
24 is even and both outputs generate the same result.
?>
Output:
The following output will appear if the script is executed without providing any query string.
The following output will appear if the script is executed with two query strings. In the output,
342356 is given as an id value and 93 is given as marks value. So, both values are printed as the
output.
Example 3: Use of ternary operator with $_POST[] variable
The following example shows the use of the ternary operator to read the values of the $_POST
variable. Create a PHP file with the following script.
An HTML form is used in the script to take two integer numbers from the user. The ternary
operator is used to check if the numbers are numeric or not, and calculate the sum of the
numbers. If any number is not numeric, the script will print the error message, and if both
numbers are numeric, then the sum of these numbers will be printed.
<?php
//Check the form values
if(isset($_POST['n1']) && isset($_POST['n2']))
{
//Use the ternary operator to check the submitted values are numeric or not
$number1 = is_numeric($_POST['n1']) ? $_POST['n1'] : "<p style='color:red'>Enter the numeric
value.</p>";
$number2 = is_numeric($_POST['n1']) ? $_POST['n2'] : "<p style='color:red'>Enter the numeric
value.</p>";
Output:
The following form will appear if the script is executed from the server. In the output, 67 and 56
are given as the number values.
The following output will appear after clicking the submit button. If the submit button is pressed
by keeping any field empty, then an error message will be displayed.
<?php
//Check the submitted value
if(isset($_POST['name']))
{
//Set the value
$name = $_POST['name'];
//Set the color based on the chaining ternary operator
$result = ((strcmp($name,'Mohammed Ali') == 0) ? 'Blue' :
((strcmp($name,'Nila Chowdhury') == 0) ? 'Pink' :
((strcmp($name,'Meher Afroz') == 0) ? 'Green' : 'None')));
//Print the output
echo $result != 'None' ? "<p style='color:blue'>The favorite color of $name is
<b>$result</b></p>" : "<p style='color:red'>No name is selected.</p>";
}
?>
Output:
The following output will appear after running the script from the server.
The following output will appear if ‘Nila Chowdhury’ is selected from the dropdown list.
The following output will appear if the submit button is pressed without selecting any value from
the dropdown list.
Conclusion
The various uses of the ternary operator have been explained in this tutorial using simple
examples to help new PHP coders. Null Coalescing operator can be used as the alternative of
the ternary operator in PHP 7+.