Loops: Free PHP & MySQL Tutorial
Master PHP Loop Structures with Hands-On Examples
Loop Types Overview
While Loops
Execute code repeatedly while a condition remains true. Simple conditional repetition for basic iteration needs.
For Loops
Structured loops with initialization, condition, and increment in one statement. Perfect for counting operations.
Foreach Loops
Specialized for arrays and objects. Automatically iterates through all elements without manual indexing.
This tutorial uses practical shopping scenarios to demonstrate loop concepts. Each loop type is explored through hands-on coding exercises with immediate browser testing to see results.
While Loop Implementation
Initialize Variable
Set the starting value for your loop condition variable, such as $money = 100
Define Condition
Specify the condition that must remain true for the loop to continue executing
Modify Variable
Include code within the loop that changes the condition variable to eventually end the loop
While loops only execute if the initial condition is true. If you start with $money = 1 and check $money > 1, the loop will never run.
While vs Do-While Loops
| Feature | While Loop | Do-While Loop |
|---|---|---|
| Initial Execution | Only if condition is true | Always executes once |
| Condition Check | Before execution | After execution |
| Minimum Runs | Zero | One |
While Loop Implementation
Initialize Variable
Set the starting value for your loop condition variable, such as $money = 100
Define Condition
Specify the condition that must remain true for the loop to continue executing
Modify Variable
Include code within the loop that changes the condition variable to eventually end the loop
While loops only execute if the initial condition is true. If you start with $money = 1 and check $money > 1, the loop will never run.
While vs Do-While Loops
| Feature | While Loop | Do-While Loop |
|---|---|---|
| Initial Execution | Only if condition is true | Always executes once |
| Condition Check | Before execution | After execution |
| Minimum Runs | Zero | One |
For Loop Structure
Expression 1
Initialization - runs once at the beginning to set up loop variables and starting conditions.
Expression 2
Condition - evaluated before each iteration to determine if the loop should continue running.
Expression 3
Increment/Decrement - executed at the end of each loop iteration to modify the loop variable.
Remember that the three expressions in a for loop must be separated by semicolons, not commas. This is a common syntax error for beginners.
Array Processing with Foreach
Simple Array Iteration
Use foreach($array as $value) to loop through indexed arrays and access each element
Key-Value Pairs
Use foreach($array as $key => $value) to access both array keys and values in associative arrays
Multidimensional Arrays
Nest foreach loops to iterate through arrays containing other arrays as elements
Array Types Handled
Indexed Arrays
Simple arrays with numeric indices. Foreach automatically handles the indexing for easy element access.
Associative Arrays
Arrays with named keys. Access both key names and values using key-value pair syntax.
Multidimensional Arrays
Arrays containing other arrays. Use nested foreach loops to access all levels of data.
The break command immediately stops loop execution and exits the loop entirely. Use it when you need to terminate a loop based on a specific condition being met.
Implementing Break Conditions
Add Conditional Check
Use an if statement within the loop to test for your break condition
Execute Break
When the condition is met, execute the break statement to immediately exit the loop
Break vs Continue
| Feature | Break Statement | Continue Statement |
|---|---|---|
| Effect | Exits entire loop | Skips current iteration only |
| Loop Status | Loop terminates | Loop continues with next iteration |
| Use Case | Stop processing completely | Skip specific conditions |
The modulus operator (%) returns the remainder after division. Even numbers divided by 2 have remainder 0, odd numbers have remainder 1.
Key Takeaways