PHP Conditional Statement: PHP support various conditional statements like if, if else, if elseif, switch case,ternary operator etc. In this tutorial you will get to know about these conditional statements in detail, examples will help you to learn these conditional statements.
PHP Conditional Statement: PHP support various conditional statements like if, if else, if elseif, switch case,ternary operator etc. In this tutorial you will get to know about these conditional statements in detail, examples will help you to learn these conditional statements.PHP Conditional Statements
In every language we need to put some conditional logic, that is we need to perform an action according to a condition. PHP supports if , else, switch case and conditional operator for this purpose.
PHP Conditional Statement Example 1:
<?php
$a=12;
if($a>10)
$a++;
echo "Value of \$a is:".$a;
?>
Output:
Value of $a is:13
Example 2:
<?php
$a=12;
$b=34;
if($a>$b)
echo "\$a is greater than \$b";
else
echo "\$b is greater than \$a";
?>
Output:
$b is greater than $a
Example 3:
<?php
$a=12;
$b=34;
$c=45;
if($a>$b && $a > $c)
echo "\$a is the greatest";
else if($b>$c )
echo "\$b is the greatest ";
else
0echo "\$c is the greatest";
?>
Output:
$c is the greatest
Example 4:
<?php
$a=1;
2switch($a)
{
case 0:
3echo"Zero";
break;
case 1:
4echo "One";
break;
case 2:
5echo "Two";
break;
default:
6echo "Other";
}
?>
Output:
One
Example 5:
<?php
$a=143;
$b=1233;
$c=33;
9$d=(($a>$b)?(($a> $c)? $a : $c):(($b>$c)? $b : $c));
echo "<b> Greatest no. is: </b>".$d;
?>
Output:
Greatest no. is:1233