PHP If-Else, Switch Case and shorthand Ternary operator example

Filed Under: PHP

Sometimes we need to perform different actions based on the decision, PHP provides several conditional statements that can be used for this.

  1. If statement – its used when you need to execute the piece of code only when the condition is true.
  2. If-Else statement – its used to execute a piece of code when the condition is true or another piece of code when condition is false.
  3. If-Else-If Statement – its used when there are multiple code that can be executed based on the condition, as soon as one of the condition is true and its code block is executed, control comes out of the statement.
  4. Switch statement – Its same as if-else-if statement but it make code cleaner.
  5. Ternary Operator – Ternary operator provides a shorthand way to write all the above conditional statements. If the condition is not very complex, its better to use ternary operator to reduce the code size but for more complex conditions, it can become confusing. The syntax is (Condition) ? <Condition=True>:<Condition=False>

Here is an example PHP script showing usage of all the conditional statement and using ternary operator to implement the same logic in very small size of code.



//if condition example
	echo "value of a is 15";
	echo "<br>";

//if-else condition example
if($str1 == "Pankaj"){
	echo "Hi Pankaj";
	echo "<br>";
}else {
	echo "Hi There!";
	echo "<br>";

//if-else-if example

if($str == "Pankaj"){
	echo "Hi Pankaj";
	echo "<br>";
}else if($str == "David"){
	echo "Hi David";
	echo "<br>";
} else{
	echo "Hi There!";
	echo "<br>";

//switch statement example

switch ($color){
	case "Red":
		echo "Red Color";
		break; //for breaking switch condition
	case "Green":
		echo "Green Color";
		echo "Neither Red or Green Color";
echo "<br>";

//PHP ternary operator example

// implementing the above if example with ternary operator
echo ($a == 15) ? "value of a is 15"."<br>":"";

// implementing the above if-else example with ternary operator
echo ($str1 == "Pankaj") ? "Hi Pankaj"."<br>" : "Hi There!"."<br>";

//implementing above if-else-if example with ternary operator
echo ($str == "Pankaj") ? "Hi Pankaj"."<br>" : (($str == "David") ? "Hi David"."<br>" : "Hi David"."<br>");

//implementing switch statement with ternary operator
echo ($color == "Red") ? "Red Color" : (($color == "Green") ? "Green Color" : "Neither Red or Green Color");

Output of the above PHP script is:

value of a is 15
Hi Pankaj
Hi David
Red Color
value of a is 15
Hi Pankaj
Hi David
Red Color

Further reading: PHP Operators


  1. Rajesh says:

    PHP is not the same language which was 10 yrs back. With new releases, PHP is improving day by day and a ternary operator is no exception for that.
    Replacement of if-else statement was ternary operator (most of the languages supports that) then ?: shorthand and then Null Coalescing Operator / ?? operator.
    Good to see improvement day by day.

  2. Amit says:

    Wow, great way of using ternary operator.

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages