PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
"for" Statements
This section describes how 'for' statements works in PHP. One or more statements are repeatedly executed as long as the specified condition is true. 'for' statements has extra expressions for loop initialization and loop control update.
The "for" statement is the most complex loop statement in PHP. There are 4 forms of "while" statements supported in PHP:
1. Empty-statement "for":
for (initial_expression; stop_condition; repeating_expression);
2. Single-statement "for":
for (initial_expression; stop_condition; repeating_expression) statement;
3. Multi-statement "for":
for (initial_expression; stop_condition; repeating_expression) { statement; statement; ... }
4. Multi-statement "for ... endfor":
for (initial_expression; stop_condition; repeating_expression): statement; statement; ... endfor;
All forms of "for" statements are executed like this:
Step 1: "initial_expression" is evaluated unconditionally. The "initial_expression" is usually used to assign the initial value to a loop control variable, like $i=0.
Step 2: Evaluate "stop_condition" as a Boolean value. The "stop_condition" is usually a comparison expression against the loop control variable, like $i<100.
Step 3: If "stop_condition" returns TRUE, continue with Step 5.
Step 4: If "stop_condition" returns "FALSE", terminate the loop.
Step 5: Execute the statement or statements enclosed in the loop.
Step 6: Evaluate "repeating_expression". The "repeating_expression" is usually an expression to increment the value of the loop control variable, like $i++.
Step 7: Continue with Step 2.
If you want to terminate the loop early, you can use the "break" statement inside the loop, which will terminate the loop immediately.
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
►Loop Statements - "while", "for", and "do ... while"
"break" and "continue" Statements
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS