PHP tutorial Conditions


Learn PHP Conditions

 
 

 

Using Conditional Statements in PHP

Conditional statements checks for the condition. Based on the condition, it selects the statements to execute. PHP provides different types of conditional Statements. In this tutorial, we are going to discuss various conditional statements with syntax and examples. All the conditions uses comparison operators also called as relational operators. And also sometimes uses logical operators to combine one or two conditions while comparing. If no idea on these operators, please visit the tutorial on operators in PHP.

  Move to Top

Table of contents

  1. if() statement in PHP.

  2. if..else   statement in PHP.

  3. if..else if.. else   statement in PHP.

  4. ternary statement in PHP.

  5. switch case  statement in PHP.

 

1. if statement.

Used to check the given expression  is

 
  • Yes/No
  • True/False
  • Greater/Less than ..etc.
 

Syntax:

if(condition)

{

    ...

    ...

}

ex1: 
 
<?php
 
$a = 5
 
if($a > 10)
{
    print ("A is greater than 10");
}
 
?>

 

 TOP

2. if..else statement in PHP.

First checks the condition. If it is true, it executes the if block, otherwise it executes the else block.

ex1: 
 
<?php
 
$a = 5$b = 6
 
if($a > $b)
{
    print ("A is greater");
}
else
{
    print ("B is greater");
}
 

?>
ex2:
<?php
 
$user= "admin";
$pwd = "admin";
if($user == "admin" and $pwd == "admin")
    print "success";
else
    print "invalid";
 
 ?>

 Move TOP

3. if..else if .. else in PHP:

It also called as if else ladder. Generally, it is used to compare multiple conditions. We can use it to compare a given number comes in which range. It's going on checking the given value with each condition until  it satisfies the condition. If not met any condition, it executes the else part. 

 
syntax:
 
if(condition)
    {
     ....
     ....
    }
    else if(condition)
    {
     ...
    }
    else if(condition)
    {
     ...
     ...
    }
    else
    {
     ...
    }
 
 
Ex: 
 
 
<?php
 
$a = 5
$b = 6;  
$c = 7;
 
if($a>$b and $a>$c)
 
    print " <br> $a is big";
 
else if($b>$a and $b>$c)
 
    print " <br> $b is big";
 
else
 
    print " <br> $c is big";
 
 ?> 
 
 
 
 
 Ex2:
 
<?php
 
$amt=450;
 
if ($amt>=800 and $amt <=1000)
{
    print "<br />Dicount = 10%";
}
elseif ($amt>=700 and $amt <800)
{
    print "<br />Dicount = 8%";
}
elseif ($amt>=500 and $amt<700)
{
    print "<br /> Discount = 6%"; }
else{
    print "<br />No Discount";
}

 

?> 

  TOP Menu

4. Ternary if  in PHP

Ternary if  is used in place of if..else.  Because it is evaluated faster than if..else. It is a inline statement. It uses two operators(?,:).

 

<?php

$a=15;
$b=74;


$c = $a>$b ? " A is big" : " B is big"; //is inline conditional statement. faster than if-else  

print $c;


?>

 

5.  Switch() Statement in PHP: 

Switch is a multi conditional branching statement. It allows the user to select one among multiple choices. His choice will be compared with multiple case options, and only the matched case block is to be executed, and the other choices will be ignored. If not matched with any of the case options, then it executes the default block.

Syntax:

switch(variable)
{
case option1:
....
break;
case option2:
....
break;
case option3:
....
break;
case option4:
....
break;
default:
    print default message

<?php

$n=7;
switch ($n)
{
case 1:
    print "one";
break;
case 2:
    print "Two";
break;
case 3:
    print "Three";
break;
case 4:
    print "Four";
break;
default:
    print "No is not in the list";
}

 
 

  GoTOP

 
 //example 2
 
 
echo "<br>";

$country="USA";

switch ($country)
{
case "in":
    print "India";
break;
case "USA":
    print "USA";
break;
case "UK":
    print "Briton";
break;
case "fr":
    print "France";
break;
default:
    print "Wrong choice";
}
?>

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP