PHP HOME
PHP Introduction
PHP SETUP
PHP Syntax
PHP Data Types
PHP Outputs
PHP Variable
PHP Constant
PHP Strings
PHP Operators
PHP if else if
PHP Switch
PHP while loop
PHP for loop
PHP Arrays
PHP Functions
PHP Sorting Array
PHP Form Handling
PHP Form Validation
PHP Form Required
PHP Form URL/EMAIL
PHP Form Submit
PHP Contact Form
PHP DATE & TIME
PHP INCLUDE
PHP FILE HANDLING
PHP FILE OPEN/READ
PHP FILE CREATE/WRITE
PHP FILE UPLOAD
PHP SESSION
PHP COOKIES
PHP FILTER
PHP ERROR HANDLING
PHP EXCEPTION
PHP Redirects
PHP with MySQL
PHP WITH MYSQL DB
MYSQL CONNECT
MYSQL CREATE DATABASE
SELECT DATABASE
MYSQL CREATE TABLE
MYSQL INSERT DATA
MYSQL SELECT DATA
MYSQL UPDATE DATA
MYSQL DELETE DATA
MYSQL ORDER
MYSQL LIMIT
MYSQL LAST ID
When you know in advance that how many times the PHP part of code should run, at that time we use for loop.
Syntax of for loop
for (initialization; condition; increment) {
the code will be executed till condition will be true;
}
Example of while loop
Initially, $a variable is initialized by 1.The for loop starts to execute the code as long as $a is less than 10 and $a will increase by 1 every time loops run.
<!DOCTYPE html>
<html>
<body>
<?php
for($a = 1; $a < 10; $a++) {
?>
<p><?php echo $a." Time"; ?> : <?php echo $a . ' is less than 10';?></p>
<?php
}
?>
</body>
</html>
The above code produces the following results
Outputs:
1 Time: 1 is less than 10
2 Time: 2 is less than 10
3 Time: 3 is less than 10
4 Time: 4 is less than 10
5 Time: 5 is less than 10
6 Time: 6 is less than 10
7 Time: 7 is less than 10
8 Time: 8 is less than 10
9 Time: 9 is less than 10