PHP Control Structures

PHP (Hypertext Preprocessor) is a widely-used open-source scripting language, especially suited for web development. It allows developers to create dynamic content and interact with databases. One of the key aspects of PHP programming is the use of control structures. These structures guide the flow of program execution based on conditions or repeated tasks.

Types of Control Structures

Control structures in PHP are mainly of three types: conditional statements, loop statements, and jump statements.


Conditional Statements

Conditional statements execute certain blocks of code depending on conditions.

if Statement

The if statement runs a block of code if a condition is true.

Syntax

if (condition) {
  // code to execute if condition is true
}

Example

$t = date("H");
if ($t < "20") {
  echo "Have a good day!";
}

Output:
Have a good day!

if…else Statement

The if…else statement executes one block if the condition is true, another if false.

Syntax

if (condition) {
  // code if true
} else {
  // code if false
}

Example

$t = date("H");
if ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}

Output:
Have a good day!

if…elseif…else Statement

For multiple conditions, use if…elseif…else to execute different code blocks.

Syntax

if (condition1) {
  // code if condition1 true
} elseif (condition2) {
  // code if condition2 true
} else {
  // code if all false
}

Example

$t = date("H");
if ($t < "10") {
  echo "Have a good morning!";
} elseif ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}

Output:
Have a good morning!


php control structures


switch Statement

The switch statement selects one block of code to execute among many cases based on a variable.

Syntax

switch (variable) {
  case value1:
    // code
    break;
  case value2:
    // code
    break;
  default:
    // code if no match
}

Example

$favcolor = "red";
switch ($favcolor) {
  case "red":
    echo "Your favorite color is red!";
    break;
  case "blue":
    echo "Your favorite color is blue!";
    break;
  default:
    echo "Your favorite color is neither red, nor blue!";
}

Output:
Your favorite color is red!


Loop Statements

Loops repeat a block of code while a condition is true. PHP has several loop types:

  • while — loops while condition true

  • do…while — loops once, then repeats if condition true

  • for — loops a fixed number of times

  • foreach — loops through array elements

while Loop

Runs the block while the condition is true.

Syntax

while (condition) {
  // code
}

Example

$x = 1;
while ($x <= 5) {
  echo "The number is: $x <br>";
  $x++;
}

Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5


loop structure


do…while Loop

Executes once before checking the condition.

Syntax

do {
  // code
} while (condition);

Example

$x = 1;
do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);

Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

for Loop

Useful when the number of iterations is known.

Syntax

for (init; condition; increment) {
  // code
}

Example

for ($x = 0; $x <= 10; $x++) {
  echo "The number is: $x <br>";
}

Output:
The number is: 0 … up to 10

foreach Loop

Used for arrays, loops over each element.

Syntax

foreach ($array as $value) {
  // code
}

Example

$colors = array("red", "green", "blue");
foreach ($colors as $value) {
  echo "$value <br>";
}

Output:
red
green
blue


Suggested Image Idea:
Table showing an array and arrows indicating foreach loop traversing each element.


Jump Statements

Jump statements change the flow abruptly.

  • break — exits a loop

  • continue — skips to next loop iteration

  • goto — jumps to another label (rarely used)


Conclusion

Control structures in PHP are essential for writing dynamic and flexible programs. Mastering them helps create efficient code that responds to different conditions and repeats tasks effectively.

Â