Share on Google+Share on Google+

PHP Conditional Statement

Advertisement
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

 

 

Advertisements

Posted on: November 5, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.