This document discusses various PHP looping structures including for, while, do-while, foreach loops as well as break and continue statements. It provides examples of using each loop type to iterate through arrays and print output. Key looping constructs covered are for loops to iterate a set number of times, while loops to execute code while a condition remains true, do-while loops which execute code once then check the condition, and foreach loops used specifically to iterate over arrays.
“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.
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.
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.