- 5 years ago
- Zaid Bin Khalid
- 7,981 Views
-
7
The PHP also supports loops just like the other languages. There are 4 types of loop in PHP. In this tutorial, I will explain to you the syntax and usage of loops in PHP.
- for loop
- do-while loop
- while loop
- foreach loop
What is the FOR LOOP?
The for loops are the most complex loops in PHP and this loop is supported by PHP4, PHP5, PHP7. This type of loops is used when the user knows in advance, how many times the block needs to execute. The syntax is given below.
for(initialization expression; test condition; update expression){
//code to be executed
}
<?php
/* example 1 */
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
/* example 2 */
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
/* example 3 */
$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
/* example 4 */
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
The above code is the explanation of for loop you can define for loop like the above method. You can also iterate through arrays like in the example below.
<?php
/*
* This is an array with some data we want to modify
* when running through the for loop.
*/
$people = array(
array('name' => 'user 1st', 'code' => 856412),
array('name' => 'user 2nd', 'code' => 215863)
);
for($i = 0; $i < count($people); ++$i) {
$people[$i]['random code'] = mt_rand(000000, 999999);
}
print"<pre>";
print_r($people);
print"</pre>";
?>
The output will be:
Array
(
[0] => Array
(
[name] => user 1st
[code] => 856412
[random code] => 297519
)
[1] => Array
(
[name] => user 2nd
[code] => 215863
[random code] => 286503
)
)
What is DO-WHILE?
The do-while loop is very similar to the while loop the only difference is in do-while the condition is checked at the end of each iteration. The first iteration of do-while is must run which is also the main condition of the do-while loop and this loop is supported by PHP4, PHP5, PHP7.
$i = 0;
do {
echo $i;
$i++;
} while ($i <= 5);
The Below code will explain to you the difference between the while and do-while. The do-while first execute the statement then go for the condition.
<!-- if you write with WHILE -->
<?php
$i = 100;
while ($i < 10) :
echo "\$i is $i.";
endwhile;
?>
<!-- if you write with DO-WHILE -->
<?php
$i = 100;
do {
echo "\$i is $i.";
} while ($i < 10);
?>
What is WHILE LOOP?
The while loop runs until the condition becomes FALSE. It is just like the entry control loop and this loop is supported by PHP4, PHP5, PHP7. The statement is given below.
//Define while loop in PHP
while (expr)
statement
// Also define as in PHP
while (expr):
statement
...
endwhile;
The expiration in the while loop must be TRUE. Below are the examples of a while loop. You can also use a while loop with the SQL Query.
<?php
/* Example test 1 */
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
/* Example test 2 */
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
The output will be 1 to 10. The while loop is also used to fetch query data in PHP as shown in the below code.
<?php
$sql = "SELECT * FROM YOUR_TABLE WHERE 1";
if($result = $mysqli->query($sql)){
while($obj = $result->fetch_object()){
//Your loop code will be here
}
}
$result->close();
?>
What is the FOREACH loop?
The foreach loop only works for array and objects if you try to run this loop with variables you will get an error. So, the foreach construct provides an easy way to iterate over arrays. This is also supported by PHP4, PHP5, PHP7. There are two types of the syntax of foreach loop that is given below.
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
The complete example of foreach with array implementation is given below.
<?php
$array = array(1, 2, 3, 4);
foreach($arr as $value){
$value = $value * 2;
echo $value.'<br>';
}
foreach ($array as $key => $value) {
echo "{$key} => {$value} <br>";
}
?>
In the above example, the $key is the array index value.
- 5 years ago
- Zaid Bin Khalid
- 7,981 Views
-
7