PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax:

for (initialization; condition; increment/decrement ) {
code to be executed;
}
Example
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
echo " Print counting using for loop"."<br>";
for($i=1;$i<=5;$i++){
echo $i."<br>";
}
?>
</body>
</html>
Output Print counting using for loop 1 2 3 4 5 PHP Nested For loop PHP Nested for loop is used inside the loop that is called nested for loop. Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
echo " Print prymid using Nested for loop"."<br>";
for($i=1;$i<=5;$i++){
            for($j=1;$j<=$i;$j++){
echo "*";
            }
            echo "<br>";
}
?>
</body>
</html>
Output Print pyramid using Nested for loop * ** *** **** *****



ADVERTISEMENT
ADVERTISEMENT