php tutorial - PHP For Loop - php programming - learn php - php code - php script
- For loop statement is typically used to execute a block of code for certain number of times.
- for loop allows the user to put all the loop-related statements (which is INITIALIZER; CONDITION; INCREMENTOR or DECREMENTOR) in one place, which is similar to C language.
![For loop](https://wikitechy.com/php/img/php-images/for-loop.gif)
Learn PHP - PHP tutorial - For loop - PHP examples - PHP programs
php programming Syntax :
for (initialization counter; test counter; increment counter)
{
code to be executed;
}
click below button to copy the code. php tutorial - team
Syntax Explanation :
- initialization: It is used to initialize the counter variables.
- test counter: Evaluate for each loop iteration. If it evaluates to be TRUE, the loop continues. If it evaluates to be FALSE, the loop ends.
- increment : It increase the loop counter with a new value. It is evaluating at the end of each iteration.
- execute the statement: It executes the php statements.
![php - php 7 - php tutorial - php framework tutorial - php examples - php sample code - php basics - php web development - php components - php project - php technology - learn php - php online - php programming - php program - php code - html code - embedded php in html - php for loop](https://wikitechy.com/php/img/php-images/for loop1.jpg)
learn php Sample Code :
<!DOCTYPE html>
<html>
<head>
<title>For-Loop</title>
</head>
<body>
<?php
for ($x = 0; $x <= 15; $x++)
{
echo "The serial number is: $x <br>";
}
?>
</body>
</html>
click below button to copy the code. php tutorial - team
![php - php 7 - php tutorial - php framework tutorial - php examples - php sample code - php basics - php web development - php components - php project - php technology - learn php - php online - php programming - php program - php code - html code - embedded php in html - web server - php syntax - php function - php for loop](https://wikitechy.com/php/img/php-images/php-for-loop.png)
php for beginners Code Explanation :
![Code Explanation for For Loop In PHP Code Explanation for For Loop In PHP](https://wikitechy.com/php/img/php-images/code-explanation-php-for-loop.png)
- Here for ($x = 0; $x <= 20; $x++) specifies $x = 1, which means the expression, ($x<= 20), is true. Therefore, the echo statement is executed, and $x gets incremented by 1 and becomes 2.$x = 2, which means the expression, ($x <= 20), is true. Therefore, the print statement is executed till the value hits 20, and $x gets incremented by 2 and becomes 3.
php coding Sample Output :
![output for For Loop In PHP output for For Loop In PHP](https://wikitechy.com/php/img/php-images/output-for-php-for-loop.png)
- Here in this output the serial numbers : 0,1,2,3,4,5,6 will be printed until it reaches the last element “6”.