Home Tutorial Php Phpbasics Tutorial PHP Conditional Statement

 
 

PHP Conditional Statement
Posted on: November 5, 2009 at 12:00 AM
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

    echo "\$c is the greatest";

?>

Output:

$c is the greatest

Example 4:

<?php

$a=1;

switch($a)

{

case 0:

    echo"Zero";

    break;

case 1:

    echo "One";

    break;

case 2:

    echo "Two";

    break;

default:

    echo "Other";

}

?>

Output:

One

Example 5:

<?php

$a=143;

$b=1233;

$c=33;

$d=(($a>$b)?(($a> $c)? $a : $c):(($b>$c)? $b : $c));

echo "<b> Greatest no. is: </b>".$d;

?>

Output:

Greatest no. is:1233

 

 

Related Tags for PHP Conditional Statement:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.