PHP Logical Structures and Conditional Statements: Sequence, Selection, and Loop Constructs
This article introduces PHP’s logical structures—including sequential execution, selection statements like if, if‑else, and switch, as well as loop constructs such as for, while, and do‑while—providing syntax explanations and practical code examples for each.
As the web continues to evolve, PHP remains a popular, powerful language for backend development, supporting object‑oriented programming as well as a rich set of logical structures and conditional statements that simplify coding tasks.
In PHP, logical structures are defined using curly braces {} to group statements, making code clear and readable. The main categories are sequential structures, selection structures, and loop structures.
Sequential Structure – code executes line by line in the order written. The following example demonstrates a simple sequential flow:
<code>$a = 1;
$b = 2;
$c = $a + $b;
echo $c;</code>The script assigns values to $a and $b , adds them, stores the result in $c , and outputs the sum.
Selection Structure – the program chooses different execution paths based on conditions, using if , if else , and switch case statements.
if statement syntax:
<code>if (condition) {
// code to execute when condition is true
}</code>Example:
<code>$age = 20;
if ($age >= 18) {
echo "You are an adult!";
}</code>The script checks whether $age is at least 18 and prints a message if the condition holds.
if‑else statement syntax:
<code>if (condition) {
// code block 1
} else {
// code block 2
}</code>Example:
<code>$age = 17;
if ($age >= 18) {
echo "You are an adult!";
} else {
echo "You are still a minor!";
}</code>Depending on the value of $age , the script outputs either an adult or minor message.
switch case statement syntax:
<code>switch (expression) {
case value1:
// code block 1
break;
case value2:
// code block 2
break;
default:
// code block 3
}</code>Example:
<code>$x = 1;
switch ($x) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
default:
echo "Not Monday or Tuesday";
}</code>The script prints the day name based on the value of $x .
Loop Structure – repeats a block of code multiple times. PHP provides for , while , and do while loops.
for loop syntax:
<code>for (initialization; condition; increment) {
// code to execute each iteration
}</code>Example:
<code>for ($i = 1; $i <= 10; $i++) {
echo $i;
}</code>This prints numbers 1 through 10.
while loop syntax:
<code>while (condition) {
// code to execute each iteration
}</code>Example:
<code>$i = 1;
while ($i <= 10) {
echo $i;
$i++;
}</code>The result is identical to the for loop above.
do while loop syntax:
<code>do {
// code to execute
} while (condition);
</code>Example:
<code>$i = 1;
do {
echo $i;
$i++;
} while ($i <= 10);
</code>This also outputs numbers 1 to 10, guaranteeing the block runs at least once.
In summary, PHP’s logical structures and conditional statements give developers a flexible toolkit to implement a wide range of functionalities, making mastery of sequence, selection, and loop constructs essential for effective web development.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.