Control Statement in PHP | If | If else | If else If with Examples

Hello Friend how are you i hope you all are doing well. Today in this post we will learn about control statements like what is if statement , what is if else statement , what is if else if statement and nested if with examples. If you want to learn PHP in very simple easy way then this post will help you definitely.

Let's Start

1. If Statement

It is used to test the condition and the result is based on the condition.
If the condition is true its body will execute otherwise does not execute.
Example

<?php
$x=2;
if($x < 10)
{
//this part will execute if the condition is true
echo "x is less than 10<br>";
}
echo "The value of x is $x";
?>
//output
x is less than 10
The value of x is 2

Program: Show Result according to percent

<?php
$marks=55;
if($marks >= 60)
{
//this part will execute if percent is greater than  or equal to 60
echo "First division<br>";
}
if($marks < 60 && $marks >= 45)
{
//this part will execute if percent is between 59 and 45
echo "Second division<br>";
}
if($marks<45 && $marks>=33)
{
//this part will execute if percent is between 44 and
echo "Third division<br>";
}
if($marks<33)
{
//this part will execute if percent is less than  33
echo "Sorry!! You are Fail!!<br>";
}
?>
//output
Second division

2. If else Statement


It is used to test the condition and gives the output in both situation either condition true or false.
If the condition is true body of if will execute otherwise body of else execute.
Example:

<?php
//Variable definition
$x=10;
if($x>5)
{
 echo "$x is greater than 5";
}
else
{
echo "$x is less than 5";
}
?>
//output
10 is greater than 5

Program 1:Voting Program

<?php
//Variable definition
$age=35;
if($age>5)
{
 echo "You are eligible for voting";
}
else
{
echo "You are not eligible for voting";
}
?>
//output
You are eligible for voting

Program 2:Login Program

<?php
//click event
if(isset($_POST['btn']))
{
//passing username textbox value to variable $un
$un=$_POST['u'];
//passing password textbox value to variable $pwd
$pwd=$_POST['p'];
if($un=="abc@gmail.com" && $pwd=="1234")
{
 echo "Login success";
}
else
{
echo "Login failed";
}
}
?>
<form  method="post" enctype="multipart/form-data">
<table border="0" width="15%" cellpadding="5px" style="border:2px solid red">
<tr><td>Username</td><td><input type="text" name="u" /></td></tr>
<tr><td>Password</td><td><input type="password" name="p" /></td></tr>
<tr><td></td><td><input type="submit" name="btn" value="Login" /></td></tr>
</table>
</form>
Output:


3. If else if Statement (if else ladder)


It is used to test the condition.
If executes only one condition at a time.
The condition which is true first from the top will execute.
Example:

<?php
//Variable definition
$x=10;
//condtion false
if($x<5)
{
 echo "Hello1";
}
//condition true
else if($x>5)
{
echo "Hello2";
}
//condition true
else if($x<15)
{
echo "Hello3";
}
else
{
echo "Hello4";
}
?>
//Output
Hello2

Program: Show result according to percent

<?php
$marks=55;
if($marks>=60)
{
//this part will execute if percent is greater than  or equal to 60
echo "First division<br>";
}
else if($marks>=45)
{
//this part will execute if percent is between 59 and 45
echo "Second division<br>";
}
else if($marks>=33)
{
//this part will execute if percent is between 44 and
echo "Third division<br>";
}
else
{
//this part will execute if percent is less than  33
echo "Sorry!! You are Fail!!<br>";
}
?>
//output
Second division

4. Nested If Statement


It is used to test the condition.
One if inside another if is called nested if.
Example 1:

<?php
//variable definition
$no=5;
//true condition	
if($no>=2)
{
  //false condition
  if($no<3)
  {
  echo "Hello1<br>";
  }
  echo "Hello2";
}
?>
//output
Hello2

Example 2:
<?php
//variable definition
$no=5;
//true condition	
if($no>=2)
{
  //true condition
  if($no>3)
  {
  echo "Hello1<br>";
  }
  echo "Hello2";
}
?>
//output
Hello1
Hello2

Example 3:
<?php
//variable definition
$no=5;
//false condition	
if($no<2)
{
  //true condition
  if($no>3)
  {
  echo "Hello1<br>";
  }
  echo "Hello2";
}
?>
//output
 no output because first condition(outside if)  is false

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks

Post a Comment

0 Comments