Share on Google+Share on Google+

PHP If Construct

Advertisement
In this tutorial we will study about if control structure.What is the use of if and how to implement are discussed in this tutorial.Examples in this tutorial will make it more clear.

The If Construct:

The if construct is present in most of the famous computer languages, PHP uses the if construct to check the execution of code fragments. The if structure in PHP is similar to the
C language.

The output of the condition is either true or false, if the output of the condition is true then the next line or couple of lines will be executed otherwise not.  To display more than one line we need to put together all the statements within curly braces.

We can place more than one if construct inside another if statement and it is called nested if.

Example:

<?php

$a=34;

$b=45;

if($a<$b)echo"\$a is smaller";

?>

Output:

$a is smaller

Example:

<?php

$a=34;

$b=34;

if($a<$b)echo"\$a is smaller<br/>";

if($a>$b)echo"\$a is bigger<br/>";

if($a==$b)echo"\$a is equal to \$b<br/>";

?>

Output:

$a is equal to $b

Example:

<?php

$a=34;

$b=3;

$c=45;

if($a>$b)

{

if($a>$c)

echo "\$a is greatest";

}

if($b>$a)

{

if($b>$c)

echo "\$b is greatest";

}

if($c>$a)

{

if($c>$b)

echo "\$c is greatest";

}

?>

Output:

$c is greatest

 

 

Advertisements

Posted on: March 25, 2010 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.