0% found this document useful (0 votes)
19 views

PHP Loops

The document discusses PHP loops including the while loop, do-while statement, for loop, and foreach loop. The while loop executes code while a condition is true. The do-while statement executes code once then repeats while the condition is true. The for loop is used when the number of iterations is known in advance. The foreach loop iterates through arrays, outputting each value. Examples are provided for each loop type.

Uploaded by

matalas12345
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

PHP Loops

The document discusses PHP loops including the while loop, do-while statement, for loop, and foreach loop. The while loop executes code while a condition is true. The do-while statement executes code once then repeats while the condition is true. The for loop is used when the number of iterations is known in advance. The foreach loop iterates through arrays, outputting each value. Examples are provided for each loop type.

Uploaded by

matalas12345
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

PHP Loops

The while Loop


The while loop executes a block of code while a condition is true.
Example:

<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>

Output:

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

The do...while Statement


The do...while statement will always execute the block of code once, it will then check
the condition, and repeat the loop while the condition is true.
Example:

<html>
<body>
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
</body>
</html>

Output:
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6

The for Loop


The for loop is used when you know in advance how many times the script should run.
Example:
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body>
</html>

Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

The foreach Loop


The foreach loop is used to loop through arrays.
Example:
<html>
<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
</body>
</html>

Output:
one
two
three
AMA Computer College
Santa. Cruz, Laguna

MAJEL 3

John Carlo Borigas September 15, 2010


4-BSIT Mrs. Angelica Aguino

You might also like