PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The for
loop is one of the most common control flow structures in PHP, and is used when you want to perform a block of code a specific number of times.
Here's the basic syntax of a for
loop:
for (initialize; condition; increment) { // code to be executed; }
Example 1: Basic Usage
for ($i = 0; $i < 10; $i++) { echo $i; }
In this example, the for
loop will execute the echo
statement 10 times, printing the values from 0 to 9.
Example 2: Looping Through an Array
$fruits = array("Apple", "Banana", "Cherry"); for ($i = 0; $i < count($fruits); $i++) { echo $fruits[$i]; }
In this example, the for
loop will iterate over the $fruits
array and print each fruit on a new line.
Example 3: Decrementing Counter
for ($i = 10; $i > 0; $i--) { echo $i; }
In this example, the for
loop will count backwards from 10 to 1. The --
operator is used to decrement the counter $i
each time the loop executes.
Note: Be careful when defining the condition for the for
loop. If the condition never becomes false, the for
loop will continue indefinitely, potentially causing your script to hang or crash. Always make sure that the loop will end at some point.
How to use for loops in PHP:
for
loop in PHP:<?php for ($i = 0; $i < 5; $i++) { // Loop body echo $i . "\n"; }
PHP for loop examples:
for
loop:<?php for ($i = 1; $i <= 5; $i++) { echo "Iteration $i\n"; }
Nested for loops in PHP:
for
loops:<?php for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { echo "($i, $j) "; } echo "\n"; }
Incrementing and decrementing in PHP for loops:
<?php // Incrementing for ($i = 0; $i < 5; $i++) { echo "Incrementing: $i\n"; } // Decrementing for ($i = 4; $i >= 0; $i--) { echo "Decrementing: $i\n"; }
Using arrays with for loops in PHP:
for
loop:<?php $numbers = [1, 2, 3, 4, 5]; $count = count($numbers); for ($i = 0; $i < $count; $i++) { echo $numbers[$i] . "\n"; }
PHP foreach loop vs. for loop:
foreach
and for
loops:<?php // Using for loop $numbers = [1, 2, 3, 4, 5]; $count = count($numbers); for ($i = 0; $i < $count; $i++) { echo $numbers[$i] . "\n"; } // Using foreach loop foreach ($numbers as $number) { echo $number . "\n"; }
Looping through associative arrays in PHP:
for
loop:<?php $person = [ 'name' => 'John', 'age' => 30, 'city' => 'New York', ]; foreach ($person as $key => $value) { echo "$key: $value\n"; }
Common mistakes with PHP for loops:
<?php // Common mistake: Missing initialization for ($i < 0; $i < 5; $i++) { // Loop body echo $i . "\n"; } // Common mistake: Missing semicolon for ($i = 0 $i < 5; $i++) { // Loop body echo $i . "\n"; }
Break and continue statements in PHP for loops:
break
and continue
statements:<?php // Using break for ($i = 0; $i < 5; $i++) { if ($i === 3) { break; } echo $i . "\n"; } // Using continue for ($i = 0; $i < 5; $i++) { if ($i === 2) { continue; } echo $i . "\n"; }