CH 2
CH 2
• if…else statement
• if…elseif…else statement
• switch statement
Control Statements..(Continue)
• if Statement: This statement allows us to set a condition. On being TRUE, the
following block of code enclosed within the if clause will be executed.
• Syntax:
if(condition(){
//if TRUE then executes the code
}
• Example:
<?php
$x = 7;
if ($x > 0) {
echo "The number is positive";
}
?>
Output: The number is positive
Control Statements..(Continue)
• if…else Statement: if a condition will • <?php
hold i.e., TRUE, then the block of code $x = -12;
within if will be executed. But what if the
condition is not TRUE and we want to
perform an action? If a condition is if ($x > 0) {
TRUE then if block gets executed, echo "The number is positive";
otherwise else block gets executed.
}
• Syntax: else{
if(condition){ echo "The number is negative";
//if TRUE then execute this code }
?>
}
else{ OUTPUT:-The number is negative
//if FALSE then execute this code
}
Control Statements..(Continue)
• if…elseif…else Statement: This allows • Example:
us to use multiple if…else statements. • <?php
We use this when there are multiple • $x = "August";
conditions of TRUE cases.
if ($x == "January") {
• Syntax: echo "Happy Republic Day";
}
if(condition){
//if TRUE then execute this code elseif ($x == "August") {
echo "Happy Independence Day!!!";
} }
• Method 1 <?php
<?php $studentMarks = array(“abc" => 85,
$fruits = array("Apple", "Banana", "Orange");
“xyz" => 90, “pqr" => 78);
OUTPUT: ?>
Apple OUTPUT:
Banana Name: abc, Marks: 85
Orange
Name: xyz, Marks: 90
<?php
$count = 0;
$count++;
if ($count = = 5) {
}?>
Arrays
• Arrays in PHP that allows you to store multiple elements of similar or different data
types.
• The arrays are helpful to create a list of elements of similar types, which can be accessed
using their index or key.
● OUTPUT:
● winter, summer , spring
Arrays..(Continue)
• 2nd way..(indexed array)
$season[0]=“winter";
$season[1]=“summer";
$season[2]=“monsoon";
• OUTPUT:
● winter, summer , monsoon
Arrays..(Continue)
• Associative Array
• An array in which each element has a key-value pair, such an array is called as an
associative array.
• In this type of array, each value is identified by its associated key and not an index.
• We can associate name with each array elements in PHP using => symbol.
• There are two ways to define associative array:
• 1st way
● $person=array(“Name"=>“Janvi", “Address"=>“surat", “age"=>24);
• OUTPUT:-
• Janvi, 24
Arrays..(Continue)
• 2nd way..(Associative array)
$salary[“Janvi"]=35000;
$salary[“drashti"]=45000;
$salary[“akshay"]=20000;