PHP If Construct


 

PHP If Construct

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.

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)

0

{

if($b>$c)

echo "\$b is greatest";

1

}

if($c>$a)

{

2

if($c>$b)

echo "\$c is greatest";

}

3

?>

Output:

$c is greatest

4

 

 

Ads