Conditional Ternary Operator in PHP with Code Syntax Examples

PHP Ternary Conditional Operator

Ternary Operator is a conditional assignment in which you can assign the condition as well as either two different values that can be assigned to the target variable based on the condition.

Ternary Operator performs conditional operators. Ternary operators simplify some PHP conditional statement. But not simplify all the PHP conditional statements.

Ternary Operator is denoted by “?” symbol.

Example 1 – PHP Ternary Conditional Operator Example with Code Syntax

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = 7;
		$x = ($x<=18) ? 'Not Eligible for Vote' : 'Eligible for Vote';
		echo $x;
	?>
  </body>
</html>

In the above code, we uses ternary operator “?” to perform the task to know the value of variable $x is eligible for vote or not.

In this code, we define the value of $x is 7 and condition is if the value of $x less than 18 than print not eligible for the vote, if not then print not eligible.

PHP Ternary Conditional Operator Example with Code Syntax
Fig.1 – Ternary Conditional Operator Example Output Screenshot.
You can leave a response, or trackback from your own site.

One Response to “Conditional Ternary Operator in PHP with Code Syntax Examples”

  1. Shalon Lutke says:

    Thanks for ones marvelous posting! I seriously enjoyed reading it, you may be a great author.I will ensure that I bookmark your blog and will come back from now on. I want to encourage that you continue your great work, have a nice weekend!

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape