PHP Conditional Operators

By Kunal Kapoor
20-04-2018
494

Like any other programming language, If and switch conditional statements are used extensively in PHP also.

An If condition allows execution of the conditional code in one of two possible ways, ie if condition is true or if condition is false.

if condition is a control operator that checks a particular condition input and can perform one of two case ie

  • 1.   input condition is true

  • 2.   input condition is false


A switch condition allows execution in a branched way as per the input case conditions set.

Switch case is a multiple branched conditions. It can execute based on the input case value.

Unlike the if condition, switch can have multiple cases.

Switch condition works on a specific type of input, like a integer based conditions,char based, etc


eg


<?php

$x = 32;
$y = 867;

if($x>$y){
echo "TRUE condition: ".$x. " is greater than ".$y."<br>";
}
else{
echo "FALSE condition: ".$x. " is smaller than ".$y."<br>";
}

/// SWITCH case

$condition=3;

switch($condition){
case 1:

echo "case 1 execution <br>";
break;

case 2:
echo "case 2 execution <br>";
break;

case 3:
echo "case 3 execution <br>";
break;
}

?>


Output:

FALSE condition: 32 is smaller than 867
case 3 execution 




Related Tutorials