Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> PHP >> If…Else Statement in PHP
If…Else Statement in PHP
If…Else Statement in PHP


by Arun Singh on 9/5/2011 4:46:58 PM

Views: 2523       Comments: 0

If…Else Statement in PHP

‘If… Else’ statement is used to perform different action for different decision. Conditional (relational operator) statements are used to perform different action on basis of different decision. So we can say that ‘If…Else’ statement generally used with conditional statement.

In PHP, we have several conditional statements such as:

‘If’ statement: If statement is used to execute line of code at some specified condition.

Let’s we have an example, how to implement ‘If’ statement in php.

Syntax:

If(condition)

   code to be executed if condition is true;

Example:

<html>

     <head>

       <title> If statement</title>

     </head>

<body>

       <?php

             

              // Declares variables

              $value = 50;

 

              // if statement block is started at first specified condition

              if($value == 50)

               {

                   // code for execution

                   echo "This is if statement block at (value == 50) condition "; 

               }

             

              // if statement block is started at second specified condition

              if($value == 25)

               {

                   // execution code

                 echo "This is if statement block at (value == 50) condition ";   

               }

             

         ?>

</body>

 

</html>

Output:

If…Else Statement in PHP

From above example it is clear that if statement block is executed when specified condition will be right.

If…Else Statement: Use If…Else statement when specified condition is true or false. That is If… Else statement executes line of code even; it is in true condition or false condition.

To more understand about If…Else statement, let’s we have an example, how to implement If…else condition.

Syntax:

If(condition)

  code to be executed if condition is true;

else

  code to be executed if condition is false;

 

Example:

<html>

     <head>

       <title> If statement</title>

     </head>

<body>

       <?php

             

              //  retrieve current month name from date() function

              $month =  date("M");

      

              // if...else statement block is started

              if($month == "Aug" )

               {

                   // if above condition true

                   echo "This is if statement block ";

               }

             

              else

               {

                  // if above condition not true

                 echo " This is else statement block";

                }

         ?>

</body>

 

</html>

 

Output:

If…Else Statement in PHP

Nested if…else Statement:  Use nested if…else statement to select one of several code block to be executed.

Let’s we have an example, how to implement nested if…else statement in php.

Syntax:

If(condition)

     code to be executed if condition is true;

else if(condition)

     code to be executed if condition is true;

else if(condition)

     code to be executed if condition is true;

.......

.......

.......

else

     code to be executed if condition is false;

 

Example:

<html>

     <head>

       <title> If else if statement</title>

     </head>

<body>

       <?php

             

              // Declare variables name

              $total  = 500 ;

              $gain  = 360 ;

              $percent  = ($gain/$total )* 100;

 

              if($percent >=75)

               {

                     print "Distinction";

                }

              else if ($percent >59 && $percent <=74)

               {

                     print "First division";

                }

              else if ( $percent >=45 && $percent <=59)

                {

                     print "second Division";

                }

              else if ($percent >=33 && $percent <45 )

              {

                     echo "Third Division";

              }

              else

              {

                echo "Fail";

              }

 

         ?>

</body>

 

</html>

Output:

If…Else Statement in PHP

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 3288
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.