PHP if else Control statement tutorial with example

PHP if else Control statement tutorial with example

In this tutorial i will going to show you how to use PHP if or  if else  statements with 9 working example to use under different circumstances.

PHP if else or  Control Statement
The program listed in the earlier chapter was containing statements that proceed one after the other.  Control statement allows you to change the sequence of the instructions for execution.
PHP if statement………
The if statement is a control statement that test a particular condition. Whenever, the evaluated condition comes out to be true. Then the action and set of actions are carried out. Otherwise, the given set of action(s) is ignored. The syntax for this statement is:
If(expression)
Statement(s);
pro1.php
<?php
$sal=10000;
$bonus=0;
if($sal>=10000)
$bonus=2000;
$netsal=$sal+$bonus;
echo “Net Salary=”.$netsal;
?>
When the value of ‘$sal’ variable less than 10000 then expression carried out false and escaped next statement ,because next statement is depends on if condition.

If-else statement………..
The syntax for this statement is:
If(expression)
Statements-1
else
statement-2
Now consider the following example pro2.php so save and run it.
Pro2.php
<?php
$sal=10000;
if($sal>=10000)
$bonus=2000;
else
$bonus=1000;
$netsal=$sal+$bonus;
echo “Net Salary=”.$netsal;
?>
In the above program when the condition will false the control will place in else part. The if statement by itself will cause the execution of single statement. Group of statement will enclose in pair of braces, which is illustrated in pro3.php. so save and run pro3.php
pro3.php
<?php
$sal=10000;
if($sal>=10000)
{
$hra=($sal*20/100);
$da=($sal*10/100);
}
else
{
$hra=($sal*10/100);
$da=($sal*5/100);
}
$netsal=$sal+$hra+$da;
echo “Net Salary=”.$netsal;
?>
isset() :-   isset() is  a function. It receives one argument and return true if argument have any kind of value. Consider the following program.
Pro4.php
<?php
$num=50;
$status=isset($num);
echo $status;
?>
empty():-empty() is a function it receive one argument and return true if argument have not any kind of value. Consider the following example.
pro5.php
<?php
$num;
$status=empty($num);
echo $status;
?>
In the following program output is not controlled through submit button. Means when you will call pro6.php  some line will appear ,then after putting the value you will find some result.
pro6.php
<?php
$num1=$_POST[‘t1’];
$num2=$_POST[‘t2’];
$btn=$_POST[‘b1’];
echo “First Number=”.$num1.”<br>”;
echo “Second Number=”.$num2.”<br>”;
echo “Value of Button=”.$btn.”<br>”;
$res=$num1+$num2;
echo “Result=”.$res;
?>
<html><head><title>Form</title></head>
<body>
<form action=”” method=”post”>
First Number<input type=”text” name=”t1″><br>
Second Number<input type=”text” name=”t2″><br>
<input type=”submit” name=”b1″ value=”ADD”>
</form>
</body>
</html>
Problem of pro6.php is solved using isset() function in pro7.php wich is following.
pro7.php
<?php
$num1=$_POST[‘t1’];
$num2=$_POST[‘t2’];
$btn=$_POST[‘b1’];
if(isset($btn))
{
echo “First Number=”.$num1.”<br>”;
echo “Second Number=”.$num2.”<br>”;
echo “Value of Button=”.$btn.”<br>”;
$res=$num1+$num2;
echo “Result=”.$res;
}
?>
<html><head><title>Form</title></head>
<body>
<form action=”” method=”post”>
First Number<input type=”text” name=”t1″><br>
Second Number<input type=”text” name=”t2″><br>
<input type=”submit” name=”b1″ value=”ADD”>
</form>
</body>
</html>
Save pro8.php program  and try to understand the use of isset function with the help of example.
pro8.php
<?php
$num1=$_POST[‘t1’];
$num2=$_POST[‘t2’];
$bt=$_POST[‘b1’];//assign the value of “b1” button into “$bt”
if(isset($bt))//check the value is available or not if available then condition will true
{
$num2=$num1;
}
if(isset($_POST[‘b2’]))//here both task is performing in if() condition block
{
$tmp=$num1;
$num1=$num2;
$num2=$tmp;
}
?>
<html><head><title>Form</title></head>
<body>
<form action=”” method=”post”>
First Number<input type=”text” name=”t1″ value=”<?php echo $num1;?>”><br>
Second Number<input type=”text” name=”t2″ value=”<?php echo $num2;?>”><br>
<input type=”submit” name=”b1″ value=”COPY”>
<input type=”submit” name=”b2″ value=”SWAP”>
</form>
</body>
</html>

Use of  PHP If  else ladder with an example
<?php
$d=$_POST[‘day’];
if($d==1)
$dname=”Sunday”;
else if($d==2)
$dname=”Monday”;
else if($d==3)
$dname=”Tuesday”;
else if($d==4)
$dname=”Wednessday”;
else if($d==5)
$dname=”Thursday”;
else if($d==6)
$dname=”Friday”;
else if($d==7)
$dname=”Saturday”;
else
$dname=”Invalid No”;
?>
<html>
<head><title>Form</title></head>
<body>
<form action=”” method=”post”>
Enter Day No(1-7)<input type=”text” name=”day”><br>
Day name<input type=”text” value=”<?php echo $dname;?>”><br>
<input type=”submit” name=”b1″ value=”Show”>
</form>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion