0% found this document useful (0 votes)
15 views22 pages

CH 2

The document provides an overview of control statements and loops in PHP, detailing various conditional statements such as if, if-else, if-elseif-else, and switch, along with examples. It also explains different types of loops including for, while, do-while, and foreach, highlighting their syntax and use cases. Additionally, the document covers arrays in PHP, describing indexed, associative, and multidimensional arrays, along with examples of how to define and access their elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views22 pages

CH 2

The document provides an overview of control statements and loops in PHP, detailing various conditional statements such as if, if-else, if-elseif-else, and switch, along with examples. It also explains different types of loops including for, while, do-while, and foreach, highlighting their syntax and use cases. Additionally, the document covers arrays in PHP, describing indexed, associative, and multidimensional arrays, along with examples of how to define and access their elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Ch:2 Control statements &

working with Arrays


Control Statements
• PHP allows us to perform actions based on some type of conditions that may be
logical or comparative.
• PHP provides us with four conditional statements:
• if statement

• 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!!!";
} }

else if{//if TRUE then execute this code} else{


echo "Nothing to show";
else if{//if TRUE then execute this code}
}
else{// if FALSE then execute this code} ?>
OUTPUT:- Happy Independence Day!!!
Control Statements..(Continue)
• Example:
• switch Statement: The “switch”
performs in various cases i.e., it has <?php
various cases to which it matches the $color = "red";
condition and appropriately executes a
particular case block. switch ($color) {
case "red":
• It first evaluates an expression and then
compares with the values of each case. echo "You chose red.";
If a case matches then the same case is break;
executed. case "blue":
1. The break statement is used to stop the echo "You chose blue.";
automatic control flow into the next break;
cases and exit from the switch case. case "green":
2. The default statement contains the code echo "You chose green.";
that would execute if none of the cases break;
match. default:
echo "Unknown color.";
break;
}?>
Control Statements..(Continue)
• Ternary Operator- PHP provides a shorthand way of writing if…else, called Ternary
Operators.
• The statement uses a question mark (?) and a colon (:) and takes three operands: a condition
to check, a result for TRUE and a result for FALSE.
• Syntax: (condition) ? If TRUE execute this : otherwise execute this;
• Example:
• <?php
$age = 20;
echo ($age >= 18) ? "You are an adult." : "You are a minor.";
?>
OUTPUT:-You are an adult.
Loops
• Like other programming languages, PHP Loops are used to repeat a block of code
multiple times based on a given condition.
• Why Use Loops?
• Loops allow you to execute a block of code multiple times without rewrite the
code.
● Performing an action a specific number of times
• PHP provides several types of loops to handle different scenarios, including while
loops, for loops, do…while loops, and foreach loops.
Loops..(Continue)
• for Loop
• PHP for loop is used when you know exactly how many times you want to iterate
through a block of code. It consists of three expressions:
• Syntax: for(Initialization; Condition; Increment/Decrement){
}
• <?php
for ($num = 1; $num <= 5; $num ++) {
echo $num . " ";
} ?>
OUTPUT: 1 2 3 4 5
Loops..(Continue)
• while Loop
• The while loop is also an entry control loop like for loops. It first checks the condition at
the start of the loop and if its true then it enters into the loop and executes the block of
statements, and goes on until the condition goes false.
• Syntax: while(condition){
// code is executed
}
• <?php
$num = 1;
while ($num <= 5) {
echo $num . " ";
$num++;
}?>
Loops..(Continue)
• do-while Loop
• The do-while loop is an exit control loop which means, it first enters the loop, executes the
statements, and then checks the condition.
• Therefore, a statement is executed at least once using the do…while loop.
• Syntax: do{
// code
}while(condition);
<?php
$num = 1;
do {
echo $num . " ";
$num++;
} while ($num <= 5);
?>
Loops..(Continue)
• foreach Loop
• The foreach loop in PHP is convenient way to iterate over arrays and objects. It goes
through each element of an array one by one.
• Unlike traditional loops (like for or while), foreach automatically manages the iteration.
• Syntax:
foreach($array as $value){
//code
}
OR
foreach($array as $key => $value){
//code
}
Loops..(Continue)
• foreach Loop..(Continue) • Method 2

• Method 1 <?php
<?php $studentMarks = array(“abc" => 85,
$fruits = array("Apple", "Banana", "Orange");
“xyz" => 90, “pqr" => 78);

foreach ($fruits as $fruit) {


foreach ($studentMarks as $name => $marks) {
echo $fruit . "<br>";
echo "Name: $name, Marks: $marks <br>";
}
?> }

OUTPUT: ?>
Apple OUTPUT:
Banana Name: abc, Marks: 85
Orange
Name: xyz, Marks: 90

Name: pqr, Marks: 78


break statement
• In PHP break is used to immediately terminate the loop and the program control.
• Example:
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break; // Exit the loop when $i is 5
}
echo "Number: $i<br>";
}
?>
continue statement
• The continue statement is used within looping and to skip the current iteration and check
for the next condition.
• Example:

<?php

$count = 0;

while ($count < 10) {

$count++;

if ($count = = 5) {

continue; // Skip the iteration when $count is 5

echo "Count: $count<br>";

}?>
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.

• Types of Array in PHP


● Indexed or Numeric Arrays
● Associative Arrays
● Multidimensional Arrays
Arrays..(Continue)
• Indexed or Numeric Arrays
● PHP index is represented by number which starts from 0.
● We can store number, string etc..in the PHP array. All PHP array elements
are assigned to an index number by default.

● There are two ways to define indexed array:


● 1st way:
● $season=array(“winter", “summer", “monsoon", “autumn“, ”spring”);

● Accessing array elements


● echo $season[0] , $season[1], $season[4];

● OUTPUT:
● winter, summer , spring
Arrays..(Continue)
• 2nd way..(indexed array)
$season[0]=“winter";
$season[1]=“summer";
$season[2]=“monsoon";

• Accessing array elements


● echo $season[0] , $season[1], $season[2];

• 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);

• Accessing array elements


● echo $person[“Name”] , $person[“age”];

• OUTPUT:-
• Janvi, 24
Arrays..(Continue)
• 2nd way..(Associative array)
$salary[“Janvi"]=35000;
$salary[“drashti"]=45000;
$salary[“akshay"]=20000;

• Accessing array elements


● echo $ salary[“Janvi”] , $ salary[“drashti”];
• OUTPUT:-
• 35000, 45000
Arrays..(Continue)
• Multidimensional Array
• PHP multidimensional array is also known as array of arrays. It allows you to store
tabular data in an array.
• PHP multidimensional array can be represented in the form of matrix which is
represented by row * column.
Arrays..(Continue)
• Syntax
array (
array (elements...),
array (elements...)
... );
• Example:
$students = array(
array("name" => “abc", "grade" => "A"),
array("name" => “xyz", "grade" => “B);
• Accessing elements of the multidimensional array
• echo "Student 1: " . $students[0]["name"] . ", Grade: " . $students[0]["grade"] ”;
• OUTPUT:-
• Student 1: abc, Grade: A

You might also like