Loops in PHP - Formula Sheet
PHP Loop Syntax Structures
1. The For Loop
Syntax:
for (initialization; condition; increment/decrement) { // statements }
Parameters:
- initialization: Runs once at loop start to define the loop counter's initial state.
- condition: Checked before each loop pass. The loop body executes if true and terminates if false.
- increment/decrement: Evaluated at the end of each iteration to update the loop counter.
2. The While Loop
Syntax:
while (condition) { // statements }
Parameters:
- condition: Evaluated before every iteration. The loop body executes as long as this logical test is true.
3. The Do...While Loop
Syntax:
do { // statements } while (condition);
Parameters:
- condition: Tested at the end of each loop pass. The code block is guaranteed to run at least once. Note the required trailing semicolon.
4. The Foreach Loop
Syntax (Value Traversal):
foreach (\(array_variable as \)value) { // statements }
Syntax (Key-Value Traversal):
foreach (\(array_variable as \)key => \(value) { // statements }
Parameters:
- \)array_variable: The target array to traverse.
- \(key: Temporarily holds the key of the current element on each iteration.
- \)value: Temporarily holds the value of the current element on each iteration.
More for this chapter
Book Back Questions10 textbook MCQs · solved
Additional MCQs15 extra MCQs · solved
Practice TestInteractive · instant score
Book Back TestTest yourself on the textbook set
Additional MCQ TestTest yourself on the extra set
Study NotesConcepts & methods
More chapters in Computer Applications
View all
1 Multimedia
2 An Introduction to Adobe PageMaker
3 Introduction to Database Management System
4 PHP: Hypertext Preprocessor
5 Functions and Arrays in PHP
6 Conditional Statements in PHP
8 Forms and Files
9 Connecting PHP and MYSQL
10 Introduction to Computer Networks
11 Network Examples and Protocols
12 Domain Name System (DNS)
13 Network Cabling
14 Open Source Concepts
15 E-Commerce
16 Electronic Payment Systems
17 E-Commerce Security Systems
18 Electronic Data Interchange- EDI