OnlineCBT Logo OnlineCBT
PHP · LESSON 6

Control Structures: Conditionals and Loops

By Ratan Updated: 16 Oct 2026

Implement decision branching (if/else/switch) and iteration loops including foreach ($arr as $val).

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

1. Conditional Statements

Conditionals allow your application to execute different code blocks based on logical decisions.

A. if...else and elseif

= 90) {
    echo "Grade: A+ (Outstanding)";
} elseif ($marks >= 75) {
    echo "Grade: A (Distinction)";
} elseif ($marks >= 50) {
    echo "Grade: B (Pass)";
} else {
    echo "Grade: F (Needs Improvement)";
}

B. switch Statement

Executes one code block from several choices when comparing a single expression:

2. Loops in PHP

Loops execute a block of statements repeatedly as long as a specified condition remains true.

A. while and do...while Loops

";
    $num++;
}

// do-while always executes at least once
$count = 10;
do {
    echo "Executed once even though condition is false: $count 
"; $count++; } while ($count < 5);

B. for Loop

";
}

C. foreach Loop (Arrays)

The foreach loop is specifically tailored for traversing arrays without needing manual counter tracking:

 "New Delhi",
    "Japan" => "Tokyo",
    "France" => "Paris"
];

foreach ($capitals as $country => $city) {
    echo "The capital of $country is $city.
"; }

3. Loop Control: break and continue

  • break terminates loop execution immediately.
  • continue skips the rest of the current iteration and advances to the next step.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat