- Control statements in Php are code statements used to control the flow of execution of a program based on certain conditions or criteria.
- PHP provides various types of control statements, including:
Types of Control Statements in Php
-
-
Conditional/Decision Control Statements in Php:
-
Conditional statements allow the execution of specific code blocks based on certain conditions.
-
The most commonly used conditional statements in PhP are
if
,if-else
,if-else-elseif
, andswitch
. - If Statement
- Syntax:
- if (condition) {
// codes for the condition is true
}
- if (condition) {
- Syntax:
- If-else Statement
- Syntax:
-
-
if (condition) {
// codes for the condition is true
} else {
// codes for the condition is false
}
-
-
- If – else – else if Statement
- Syntax:
- If – else – else if Statement
-
// codes for condition1 is true
} elseif (condition2) {
// codes for condition2 is true
}
// codes for condition n is true
} else {
// codes for false condition
}
-
-
- Switch Statement
- Syntax:
- switch (expression/variable) {
case value1:
// code for expression equals value1
break;
case value2:
// code for expression equals value2
break; - case value n:
// code for expression equals value n
break;
default:
// code for false/does not match any of the values of the above case
}
- switch (expression/variable) {
- Switch Statement
-
Looping Control Statements in Php:
-
Looping statements are used to execute a block of code repeatedly until a certain condition is met/condition is true.
-
The most commonly used looping statements in PhP are
for
,while
,do-while
, andfor-each
.
-
-
For Loop Statement
Syntax:
for (initialization; terminate_condition; increment/decrement) {
// code to be executed repeatedly while the condition is true
}
While Loop Statement
Syntax:
while (condition) {
// codes for the true condition
}
Do-While Loop Statement
Syntax:
do {
// code to be executed at least once and repeatedly while the condition is true
} while (condition);
For Each Loop Statement (Used with Array mainly)
Syntax:
foreach ($array_name as $variable_name {
// code to be executed for each element in the array
}
3. Jumping Control Statements in Php:
-
-
-
Jump/jumping statements allow the programmer to change the normal flow of execution of a program suddenly.
- The most commonly used jump statements in PHP are
break
,continue
, andgoto
.
-
-
Break Statement
-
-
-
- used to exit a loop prematurely/forcibly.
-
-
Syntax:
while (condition) {
// code to be executed repeatedly while the condition is true
if (some condition) {
break;
}
}
Continue Statement
-
-
-
- used to skip the current iteration and move to the next one
-
-
while (condition) {
// code to be executed repeatedly while the condition is true
if (some condition) {
continue;
}
// code after the continue statement will not be executed for that iteration
}
-
-
-
Error handling statements allow the programmer to handle errors and exceptions that occur during the execution of a program.
-
The most commonly used error-handling statements in PHP are
try
,catch
, andthrow
.
-
-
0 Comments