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

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 

Recently uploaded (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

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.