- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP for Loop
The for loop in PHP, is used when we need to execute some block of code, multiple times. For example:
<?php for($x=1; $x<=10; $x++) echo $x, "<BR>"; ?>
The output is:
The number of times, the block of code inside the loop, will be executed, depending upon, how many times the condition of the loop, evaluates to be true. In above example, since the condition $x<=10; evaluates to be true ten times, therefore the statement echo $x, "<BR>"; has been executed for ten times.
The above program can also be written as:
<?php for($x=1; $x<=10; $x++) { echo $x; echo "<BR>"; } ?>
PHP for Loop Syntax
The syntax of for loop in PHP, is:
for(initialize; condition; update) { block of code; }
The execution of the for loop starts from initialize. This statement executed at start of the loop, and only executes at once. This is used to initialized the loop variable, for example: $x=1;
Before entering into the body of the loop, the condition; expression must be evaluated to be true.
If the condition; evaluates to be true, then block of code; will be executed.
After executing the block of code;, the program flow goes to update part of the loop, to update the loop variable.
After updating the loop variable, the condition; gets evaluated, as already told, before entering into the loop, the condition; expression must be evaluated to be true.
This process continues, until the condition; evaluates to be false.
PHP for Loop Example
Now let me create an example of for loop along with its output and in-depth description:
<?php for($num=3; $num<=30; $num=$num+3) { echo $num; echo "<BR>"; } ?>
The output produced by above PHP example on for loop, is shown in the snapshot given below:
The dry run of above example is:
- The value 3 has been initialized to the variable $num, as initially the first statement (initialize) always gets executed first and only for once
- Now since the condition $num<=30 or 3<=30 evaluates to be true
- Therefore, program flow goes inside the loop
- And using the two echo statement, first the value of $num gets printed, then a line break will be inserted using the BR tag
- Now the program flow goes to the update part, and will execute the statement available there, that is $num=$num+3
- Using the update statement, the value of $num will be incremented by 3. Therefore, now $num=6
- Again the condition $num<=30 will be evaluated with new value of $num
- That is, the condition $num<=30 or 6<=30 evaluates to be true again
- Therefore program flow again goes inside the loop
- And the value of $num that is 6, will be printed on the output, followed by a new line or line break
- Again the value of $num will be incremented by 3, using the update statement. Now $num=9
- The condition $num<=30 or 9<=30 evaluates to be true at third time
- Therefore program flow again goes inside the loop for third time
- This process continues, until the condition evaluates to be false
You can also use multiple initialize, update, and/or condition statements. For example, in the following example, I'm going to use two initialization codes for the initialize part of the loop:
<?php for($num=4, $i=1; $i<=10; $i++) echo "$num * $i = ", $num*$i, "<BR>"; ?>
The output of this example, is shown in the snapshot given below:
Note - On using multiple conditional statements for condition; part of the loop, the execution of the loop stops, when any of the conditional statement evaluates to be false.
« Previous Tutorial Next Tutorial »