Thursday, 6 August 2015

PHP STATEMENT (CONTROL STRUCTURE)-IF-ELSE-SWITCH

04:12

Concept:
 The if statement specifies a block of code to be executed if a condition is true:
Syntax:

if (condition) {
    block of code to be executed if the condition is true
}


Example:

<?php


$color="red";


if($color=="red")
{

echo "color is red";

}

?>
output:
color is red

Concept:
The else statement specifies a block of code to be executed if the condition is false:
Syntax:

if (condition) {
    block of code to be executed if the condition is true
} else { 
    block of code to be executed if the condition is false
}

IF   ELSE    EXAMPLE   :

<?php


$color="red";


if($color=="red")
{

echo "color is red";

}

else


{

echo "not red color";


}


?>

PHP - The    if...else-if ....else Statement   :
Use  the  if....elseif...else statement to specify a new condition to test, if the first condition is false.
Concept:
The else if statement specifies a new condition if the first condition is false:
Syntax:
if (condition1) {
    block of code to be executed if condition1 is true
} else if (condition2) {
    block of code to be executed if the condition1 is false and condition2 is true
} else {
    block of code to be executed if no condition matched means condtion1 is false and condition2 is false.
}



Example :

<?php


$color="red";


if($color=="red")
{

echo "color is red";

}

else  if($color=="orange")

{



echo "color is orange";

}

else

{

echo "No color:";


}






?>

output:
color is red

or  we can do  in this manner  too:

<?php

$arr = array("Excellent"=>"Green","Good"=>"blue");
$grade ="Good";
if(array_key_exists($grade,$arr))

echo $arr[$grade];


?>
The PHP   switch   Statement:
Use the switch statement to select one of many blocks of code to be executed.
Concept:
This is how it works:
  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
·        Break keyword, it breaks out of the switch block.

·        This will stop the execution of more code and case testing inside the block.

·        When a match is found, and the job is done, it's time for a break. There is no need for more testing.

·        The default keyword specifies the code to run if there is no case match


Syntax:
switch(expression)
{
    case n1:
        code block to be executed if case n1 is matched.
        break;
    case n2:
       code block to be executed if case n2 is matched.
        break;
    default:
        default code block to be execute if no case matched
        break;
}


Example :


<?php

$color="white";

switch($color)

{

case "red":
echo "color is red";

break;

case  "blue":


echo "color is blue";

break;

case "orange":

echo "color is orange";

break;


default:

echo "no color";

break;

}

?>

==============================================================================================================================================
some other practice on  statement simple example on nested if :


<?php

$a=50;
$b=40;
$c=30;

if($a>$b)
{
if($a>$c)
{
echo "a is greater";
}
}

if($b>$a)
{

if($b>$c)
{

echo "b is greater";


}
}

if($c>$a)
{

if($c>$b)
{

echo "c is greater ";
}

}

?>
output:

a is greater

============================================================================================================================================== SIMPLE APPLICATION  FOR  PRACTICE   USING SWITCH SIMPLE CALCULATOR :

(1)write code for form.html:

<html>
<head>
<title>simple calculator</title>
</head>
<body>
<form action="cal.php" method="post">
enter value of a<input type="text" name="a">
enter value of b<input type="text" name="b">
<select name="cal">
<option value="add">Addition</option>
<option value="sub">Subtraction</option>
<option value="mul">Multiplication</option>
</select>
<input type="submit" name="submit" value="cal">
</form>
</body>

</html>

(2)write  code for "cal.php" :

<?php
$cal=$_POST["cal"];
$a=$_POST["a"];
$b=$_POST["b"];
if($cal=="add")
{
$c=$a+$b;
echo"it is addition".$c;
}
else if($cal=="sub")
{
$c=$a-$b;
echo"it is subtraction".$c;
}
else if($cal=="mul")
{
$c=$a*$b;
echo"it is multiplication".$c;
}
else
{
echo"no calculation";
}

?>


OUTPUT:



AFTER CLICK ON CAL BUTTON ..






Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top