SlideShare a Scribd company logo
WEB PROGRAMMING
PHP Basics – Iterations, Looping
S.Muthuganesh M.Sc.,B.Ed
Assistant Professor
Department of Computer Science
Vivekananda College
Tiruvedakam West
ganesh01muthu@gmail.com
Arrays
An array is a special variable, which can hold more than one value at a time.
Array is differs from normal variable when it’s created and its being accessed. To store
array values in an array use square bracket ([ ]).
$a[0] = "one";
$a[1] = "two";
$a[2] = "three";
$a[3] = "four";
$a[4] = "five";
Alternatively PHP provides function that allows to create an array. The above data
inserted into an array using the function array() as follows:
$a=array(0=>”one”,1=>”two”,2=>”three”,3=>”four”,4=>”five”);
To access array values as follows
echo $a[0];
echo $a[1];
Associate Arrays
The associative arrays are very similar to arrays in term of functionality but they are
different in terms of their index. Associative array will have their index as string.
$student[‘name’]=”Ram”;
$student[‘mark’]=40;
To access associate array values as follows
echo”hi Mr”.$$student[‘name’] . “you got”.$student[‘mark’];
Conditional Statements
If else
The if...else statement allows you to execute one block of code if the specified
condition is evaluates to true and another block of code if it is evaluates to false.
Syntax
if(condition)
{
true statement;
}
else
{
False statement;
}
If else
Example program
<?php
$month=date("M");/*Returns a string formatted according to the given format */
if($month=="May")
{
echo"This month is May"."<br>";
echo"its starting stage of Summer Season";
}
else
{
echo$month;
}
?>
The elseif clause
The if-else-if-else statement lets chain
together multiple if-else statements used
conduct a serial of conditional checks
and only executes the first condition that
is met.
Syntax
if(condition1)
{
executed if condition1 is true;
}
elseif(condition2)
{
executed if the condition1 is false
and condition2 is true;
}
else
{
executed if both condition1 and
condition2 are false;
}
Example
<?php
$month=date("M");
if($month=="Jan")
{
echo"This month is January"."<br>";
echo"Numeric value is 1";
}
elseif($month=="Feb")
{
echo"This month is February"."<br>";
echo"Numeric value is 2";
}
elseif($month=="Mar")
{
echo"This month is March"."<br>";
echo"Numeric value is 3";
}
elseif($month=="Apr")
{
echo"This month is April"."<br>";
echo"Numeric value is 4";
}
elseif($month=="May")
{
echo"This month is May"."<br>";
echo"Numeric value is 5";
}
elseif($month=="Jun")
{
echo"This month is June"."<br>";
echo"Numeric value is 6";
}
else
{
echo$month;
}
?>
Switch statement
• The switch-case statement is an alternative to the if-elseif-else statement, which
does almost the same thing.
• The switch-case statement tests a variable against a series of values until it finds a
match, and then executes the block of code corresponding to that match.
Switch statement
Syntax
switch(expression)
{
case label1:
//code to be executed
break;
case label2:
//code to be executed
break;
......
default:
code to be executed if all cases are not matched;
}
Switch Example
<?php
$d=date("D");
switch($d)
{
case "Mon":
print"its monday";
echo'<body style="background-color:green">';
break;
case "Tue":
print"its Tuesday";
echo'<body style="background-color:red">';
break;
case "Wed":
echo'<body style="background-color:yellow">';
print"its Wednesday";
break;
case "Thu":
print"its Thursday";
echo'<body style="background-
color:#999900">';
break;
case "Fri":
print"its Friday";
echo'<body style="background-color:blue">';
break;
case "Sat":
print"its Saturday";
echo'<body style="background-color:ff66ff">';
break;
case "Sun":
print"its Sunday";
echo'<body style="background-color:orange">';
break;
default:
print"none of the above";
}
?>
Looping
For loop
Loops through a block of code specified number of times. The general
structure of for loop
for (initialization; test condition; increment)
{
body of the loop
}
initialize : Initialize the loop counter value state of variable to be tested,
normally done by assignment operator.
test condition: Evaluated for each loop iteration. If it evaluates to TRUE, the
loop continues. If it evaluates to FALSE, the loop ends.
increment : Increases the loop counter value
Php Basics Iterations, looping
Example
<?php
for($i=1;$i<=10;$i++)
{
echo"<b>The value is $i<br/></b>";
}
The while loop
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition is true)
{
code to be executed;
}
Here the given test condition is evaluated and if the condition is true then the body
of the loop is executed.
After the execution of the body, the test condition is once again evaluated and if it
is true, the body is executed once again.
This process of repeated execution of the body continues until the test condition
finally becomes false and the control is transferred out of the loop.
Php Basics Iterations, looping
Example
<?php
$ctr=1;
while($ctr<=16)
{
echo"<b>5X$ctr=</b>".(5*$ctr)."<br/>";
$ctr++;
}
?>
Controlling Array using while Loop
Often while loop is used to run through an array as follows
while(list($key,$val)=each($array)
{
echo “ key=> $val”;
}
Example
<?php
$array=array(‘Tamilnadu’=>’Chennai’,’Goa’=>’panji’,’Maharashtra’=>’Mumbai’);
while($list($key,$value)=each($array))
{
echo $key.”<br>”;
echo $value. “<br>’;
?>
The do..while loop
The do...while loop will always execute the block of code once, it will then check
the condition, and repeat the loop while the specified condition is true.
Syntax
do
{
code to be executed;
} while (condition is true);
Here the statement is executed, then expression is evaluated.
If the condition expression is true then the body is executed again and this process
continues till the conditional expression becomes false. When the expression
becomes false.
Php Basics Iterations, looping
Example
<?php
$i=0;
do
{
$i++;
echo $i."<br>";
}while($i<10);
?>
For each loop
The foreach loop is used to iterate over arrays.
foreach($array as $value)
{
Code to be executed;
}
Example
<?php
$name=array('Ram','Seetha','Ajay','Kumar','Pari','Karthick');//array creation
$n=1;
foreach ($name as $value)
{
echo"The name is in position $n:$value"."<br>";
$n++;
}
?>
The break statement
• The PHP break keyword is used to terminate the execution of a
loop prematurely.
• The break statement is situated inside the statement block. It gives
you full control and whenever you want to exit from the loop you can
come out. After coming out of a loop immediate statement to the
loop will be executed.
The continue statement
• The PHP continue keyword is used to halt the current iteration of a
loop but it does not terminate the loop.
• Just like the break statement the continue statement is situated inside
the statement block containing the code that the loop executes,
preceded by a conditional test.
• For the pass encountering continue statement, rest of the loop code
is skipped and next pass starts.
Thank you

More Related Content

What's hot (19)

Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
Andrew Shitov
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
brian d foy
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Roles
sartak
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
Nikita Popov
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
Andrew Shitov
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
Mark Baker
 
PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
monikadeshmane
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02
Spy Seat
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
Compare Infobase Limited
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
Britta Alex
 
Php array
Php arrayPhp array
Php array
Nikul Shah
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
Kumar
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
richambra
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
Vineet Kumar Saini
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
RajKumar Rampelli
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
brian d foy
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Roles
sartak
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
Nikita Popov
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
Mark Baker
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02
Spy Seat
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
Britta Alex
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
Kumar
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
richambra
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
RajKumar Rampelli
 

Similar to Php Basics Iterations, looping (20)

Php & my sql
Php & my sqlPhp & my sql
Php & my sql
Norhisyam Dasuki
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
Sanketkumar Biswas
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)
brian d foy
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
Hassen Poreya
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
Rakesh Mukundan
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
Lorna Mitchell
 
20220112 sac v1
20220112 sac v120220112 sac v1
20220112 sac v1
Sharon Liu
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
rani marri
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
AbhishekSharma2958
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
Bozhidar Boshnakov
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
php part 2
php part 2php part 2
php part 2
Shagufta shaheen
 
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
PHP CONDITIONAL STATEMENTS AND LOOPING.pptPHP CONDITIONAL STATEMENTS AND LOOPING.ppt
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
rehna9
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
PHP-01-Overview.pptfreeforeveryonecomenow
PHP-01-Overview.pptfreeforeveryonecomenowPHP-01-Overview.pptfreeforeveryonecomenow
PHP-01-Overview.pptfreeforeveryonecomenow
oliverrobertjames
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
monikadeshmane
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
David Golden
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)
brian d foy
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
Hassen Poreya
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
Rakesh Mukundan
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
Lorna Mitchell
 
20220112 sac v1
20220112 sac v120220112 sac v1
20220112 sac v1
Sharon Liu
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
rani marri
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
PHP CONDITIONAL STATEMENTS AND LOOPING.pptPHP CONDITIONAL STATEMENTS AND LOOPING.ppt
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
rehna9
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
PHP-01-Overview.pptfreeforeveryonecomenow
PHP-01-Overview.pptfreeforeveryonecomenowPHP-01-Overview.pptfreeforeveryonecomenow
PHP-01-Overview.pptfreeforeveryonecomenow
oliverrobertjames
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
monikadeshmane
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
David Golden
 
Ad

More from Muthuganesh S (12)

javascript.pptx
javascript.pptxjavascript.pptx
javascript.pptx
Muthuganesh S
 
OR
OROR
OR
Muthuganesh S
 
Operation Research VS Software Engineering
Operation Research VS Software EngineeringOperation Research VS Software Engineering
Operation Research VS Software Engineering
Muthuganesh S
 
Cnotes
CnotesCnotes
Cnotes
Muthuganesh S
 
CSS
CSSCSS
CSS
Muthuganesh S
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
Muthuganesh S
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
Muthuganesh S
 
Php notes
Php notesPhp notes
Php notes
Muthuganesh S
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Muthuganesh S
 
Php
PhpPhp
Php
Muthuganesh S
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
Muthuganesh S
 
Javascript dom
Javascript domJavascript dom
Javascript dom
Muthuganesh S
 
Ad

Recently uploaded (20)

"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
 
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo SlidesHow to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
Celine George
 
LDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-inLDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-in
LDM & Mia eStudios
 
LDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College VolumeLDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College Volume
LDM & Mia eStudios
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
 
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
 
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
Sritoma Majumder
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
LET´S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
LET´S PRACTICE GRAMMAR USING SIMPLE PAST TENSELET´S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
LET´S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
OlgaLeonorTorresSnch
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
 
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based EducatorDiana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
How to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 WebsiteHow to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 Website
Celine George
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdfTechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup
 
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
 
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo SlidesHow to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
Celine George
 
LDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-inLDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-in
LDM & Mia eStudios
 
LDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College VolumeLDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College Volume
LDM & Mia eStudios
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
 
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
 
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
Sritoma Majumder
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
LET´S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
LET´S PRACTICE GRAMMAR USING SIMPLE PAST TENSELET´S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
LET´S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
OlgaLeonorTorresSnch
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
 
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based EducatorDiana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
How to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 WebsiteHow to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 Website
Celine George
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdfTechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup
 

Php Basics Iterations, looping

  • 1. WEB PROGRAMMING PHP Basics – Iterations, Looping S.Muthuganesh M.Sc.,B.Ed Assistant Professor Department of Computer Science Vivekananda College Tiruvedakam West [email protected]
  • 2. Arrays An array is a special variable, which can hold more than one value at a time. Array is differs from normal variable when it’s created and its being accessed. To store array values in an array use square bracket ([ ]). $a[0] = "one"; $a[1] = "two"; $a[2] = "three"; $a[3] = "four"; $a[4] = "five"; Alternatively PHP provides function that allows to create an array. The above data inserted into an array using the function array() as follows: $a=array(0=>”one”,1=>”two”,2=>”three”,3=>”four”,4=>”five”); To access array values as follows echo $a[0]; echo $a[1];
  • 3. Associate Arrays The associative arrays are very similar to arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string. $student[‘name’]=”Ram”; $student[‘mark’]=40; To access associate array values as follows echo”hi Mr”.$$student[‘name’] . “you got”.$student[‘mark’];
  • 5. If else The if...else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false. Syntax if(condition) { true statement; } else { False statement; }
  • 6. If else Example program <?php $month=date("M");/*Returns a string formatted according to the given format */ if($month=="May") { echo"This month is May"."<br>"; echo"its starting stage of Summer Season"; } else { echo$month; } ?>
  • 7. The elseif clause The if-else-if-else statement lets chain together multiple if-else statements used conduct a serial of conditional checks and only executes the first condition that is met. Syntax if(condition1) { executed if condition1 is true; } elseif(condition2) { executed if the condition1 is false and condition2 is true; } else { executed if both condition1 and condition2 are false; }
  • 8. Example <?php $month=date("M"); if($month=="Jan") { echo"This month is January"."<br>"; echo"Numeric value is 1"; } elseif($month=="Feb") { echo"This month is February"."<br>"; echo"Numeric value is 2"; } elseif($month=="Mar") { echo"This month is March"."<br>"; echo"Numeric value is 3"; } elseif($month=="Apr") { echo"This month is April"."<br>"; echo"Numeric value is 4"; } elseif($month=="May") { echo"This month is May"."<br>"; echo"Numeric value is 5"; } elseif($month=="Jun") { echo"This month is June"."<br>"; echo"Numeric value is 6"; } else { echo$month; } ?>
  • 9. Switch statement • The switch-case statement is an alternative to the if-elseif-else statement, which does almost the same thing. • The switch-case statement tests a variable against a series of values until it finds a match, and then executes the block of code corresponding to that match.
  • 10. Switch statement Syntax switch(expression) { case label1: //code to be executed break; case label2: //code to be executed break; ...... default: code to be executed if all cases are not matched; }
  • 11. Switch Example <?php $d=date("D"); switch($d) { case "Mon": print"its monday"; echo'<body style="background-color:green">'; break; case "Tue": print"its Tuesday"; echo'<body style="background-color:red">'; break; case "Wed": echo'<body style="background-color:yellow">'; print"its Wednesday"; break; case "Thu": print"its Thursday"; echo'<body style="background- color:#999900">'; break; case "Fri": print"its Friday"; echo'<body style="background-color:blue">'; break; case "Sat": print"its Saturday"; echo'<body style="background-color:ff66ff">'; break; case "Sun": print"its Sunday"; echo'<body style="background-color:orange">'; break; default: print"none of the above"; } ?>
  • 13. For loop Loops through a block of code specified number of times. The general structure of for loop for (initialization; test condition; increment) { body of the loop } initialize : Initialize the loop counter value state of variable to be tested, normally done by assignment operator. test condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment : Increases the loop counter value
  • 16. The while loop The while loop executes a block of code as long as the specified condition is true. Syntax while (condition is true) { code to be executed; } Here the given test condition is evaluated and if the condition is true then the body of the loop is executed. After the execution of the body, the test condition is once again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body continues until the test condition finally becomes false and the control is transferred out of the loop.
  • 19. Controlling Array using while Loop Often while loop is used to run through an array as follows while(list($key,$val)=each($array) { echo “ key=> $val”; } Example <?php $array=array(‘Tamilnadu’=>’Chennai’,’Goa’=>’panji’,’Maharashtra’=>’Mumbai’); while($list($key,$value)=each($array)) { echo $key.”<br>”; echo $value. “<br>’; ?>
  • 20. The do..while loop The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. Syntax do { code to be executed; } while (condition is true); Here the statement is executed, then expression is evaluated. If the condition expression is true then the body is executed again and this process continues till the conditional expression becomes false. When the expression becomes false.
  • 23. For each loop The foreach loop is used to iterate over arrays. foreach($array as $value) { Code to be executed; }
  • 24. Example <?php $name=array('Ram','Seetha','Ajay','Kumar','Pari','Karthick');//array creation $n=1; foreach ($name as $value) { echo"The name is in position $n:$value"."<br>"; $n++; } ?>
  • 25. The break statement • The PHP break keyword is used to terminate the execution of a loop prematurely. • The break statement is situated inside the statement block. It gives you full control and whenever you want to exit from the loop you can come out. After coming out of a loop immediate statement to the loop will be executed.
  • 26. The continue statement • The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop. • Just like the break statement the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. • For the pass encountering continue statement, rest of the loop code is skipped and next pass starts.