SlideShare a Scribd company logo
1 of 27
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
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.
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.
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

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-forjavaprogrammersGiovanni924
 
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 applicationsSunil Kumar Gunasekaran
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Rolessartak
 
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 GeneratorsMark Baker
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1Dave Cross
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP ArraysAhmed Swilam
 
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02Spy Seat
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016Britta Alex
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arraysKumar
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshotsrichambra
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2RajKumar Rampelli
 

What's hot (19)

Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
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
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Roles
 
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)
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
 
PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
Php array
Php arrayPhp array
Php array
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
 

Similar to Php Basics Iterations, looping

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_04Hassen Poreya
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1Rakesh Mukundan
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
20220112 sac v1
20220112 sac v120220112 sac v1
20220112 sac v1Sharon Liu
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptxrani 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 requireTheCreativedev Blog
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHPBUDNET
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)monikadeshmane
 
Scripting3
Scripting3Scripting3
Scripting3Nao Dara
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in OptimizationDavid Golden
 
10. session 10 loops and arrays
10. session 10   loops and arrays10. session 10   loops and arrays
10. session 10 loops and arraysPhúc Đỗ
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and youmarkstory
 

Similar to Php Basics Iterations, looping (20)

Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
20220112 sac v1
20220112 sac v120220112 sac v1
20220112 sac v1
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
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
 
php part 2
php part 2php part 2
php part 2
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
 
Scripting3
Scripting3Scripting3
Scripting3
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
10. session 10 loops and arrays
10. session 10   loops and arrays10. session 10   loops and arrays
10. session 10 loops and arrays
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 

More from Muthuganesh S

More from Muthuganesh S (12)

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

Recently uploaded

History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

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 ganesh01muthu@gmail.com
  • 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
  • 14.
  • 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.
  • 17.
  • 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.
  • 21.
  • 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.