Switch to full style
:read: Start PHP with us. Includes topics to help you in php
Post a reply

PHP if statement and else if

Tue Sep 09, 2008 1:46 am

if statement :
If statement in PHP is like other programming language .if statement in your code means if something is correct do something.

Code:
<?php 
 if 
(condition )  
   
{ 
     
//do something 
    }
  


when the condition is true , the code between bracket will be implemented .

Here a simple example (true condition):

Code:

  $myname
="dodo";
   if($myname=="dodo") 
     
{
        echo "oooooh, your name is dodo";
     }
   <br/>welcome ..
 
 


the output is :
Code:

   oooooh
, your name is dodo 
   welcome 
..
 
 


Here a simple example (false condition):

Code:

  $myname
="toto";
   if($myname=="dodo") 
     
{
        echo "oooooh, your name is dodo";
     }
   <br/>welcome ..
 
 


the output is :
Code:
   welcome ..



elseif statement:


In the case your condition is false ,you can make a second check after the first if.

for example :
Code:

  $myname
="toto";
   if($myname=="dodo") 
     
{
        echo "oooooh, your name is dodo";
     }
   elseif($myname=="toto")
     {
        echo "oooooh, your name is toto";
     }

   <br/>welcome ..
 
 


the output is :
Code:

   oooooh
, your name is toto
   welcome 
..
 
 


else statement :

When a condition is false other code will be executed and it is the one between else brackets .

for example :
Code:

$myname
="msi";
   if($myname=="dodo") 
     
{
        echo "oooooh, your name is dodo";
     }
   else 
     
{
        echo "oooooh, your name is $myname";
     }

   <br/>welcome ..
 
 


the output is :
Code:
   oooooh, your name is msi
   welcome ..



ok , here a general example contain if ,elseif and else statement :
Code:

  $myname
="toto";
   if($myname=="dodo") 
     
{
        echo "oooooh, your name is dodo";
     }
   elseif($myname=="toto")
     {
        echo "oooooh, your name is toto";
     }
     else 
     
{
        echo "oooooh, your name is $myname";
      }
   <br/>welcome ..
 
 


the output is :
Code:
   oooooh, your name is toto
   welcome ..



Another example :

Code:

  $myname
="Ali";
   if($myname=="dodo") 
     
{
        echo "oooooh, your name is dodo";
     }
   elseif($myname=="toto")
     {
        echo "oooooh, your name is toto";
     }
     else 
     
{
        echo "oooooh, your name is $myname";
      }
   <br/>welcome ..
 
 


the output is :
Code:
   oooooh, your name is Ali
   welcome ..



i hope it is a good article :grin:



Post a reply
  Related Posts  to : PHP if statement and else if
 difference between break statement and a continue statement     -  
 difference between a while statement and a do statement     -  
 Need help with if statement     -  
 while Statement in php     -  
 The do...while Statement     -  
 Using the for Statement     -  
 For statement without all three statements     -  
 global statement in php     -  
 Using the continue Statement     -  
 Define statement     -  

Topic Tags

PHP Basics