PHP Conditional Statement


 

PHP Conditional Statement

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

0

    echo "\$c is the greatest";

?>

Output:

1
$c is the greatest

Example 4:

<?php

$a=1;

2

switch($a)

{

case 0:

3

    echo"Zero";

    break;

case 1:

4

    echo "One";

    break;

case 2:

5

    echo "Two";

    break;

default:

6

    echo "Other";

}

?>

7

Output:

One

Example 5:

<?php

8

$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;

?>

0

Output:

Greatest no. is:1233

 

 

1

Ads