📲 Download Utility Tools Apps

EMI Calculator

Download EMI Calculator

SOA Games

Download SOA Games

SOA Technology App

Download SOA Technology

BMI Checker

Download BMI Checker

Task Jira

Download Task Jira

Laughing Adda

Download Laughing Adda

📅 हिंदी कैलेंडर ऐप डाउनलोड करें

Download Shubhcalendar App

4. Loops (PHP)

Loops are used to repeat a block of code multiple times until a condition is met.


🔹 1. for Loop

Used when you know how many times you want to run the loop.

✅ Syntax:

for (initialization; condition; increment/decrement) {
// code
}

✅ Example:

for ($i = 1; $i <= 5; $i++) {
echo $i . "<br>";
}

🔍 Output:

1
2
3
4
5

👉 Best for: Counting loops


🔹 2. while Loop

Runs as long as the condition is true.

✅ Syntax:

while (condition) {
// code
}

✅ Example:

$i = 1;while ($i <= 5) {
echo $i . "<br>";
$i++;
}

👉 Best for: When number of iterations is unknown


🔹 3. do-while Loop

Executes the code at least once, even if the condition is false.

✅ Syntax:

do {
// code
} while (condition);

✅ Example:

$i = 1;do {
echo $i . "<br>";
$i++;
} while ($i <= 5);

👉 Key difference:

  • while → checks condition first
  • do-while → runs first, then checks

🔹 4. foreach Loop

Used to loop through arrays.

✅ Syntax:

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

✅ Example:

$colors = ["Red", "Green", "Blue"];foreach ($colors as $color) {
echo $color . "<br>";
}

🔍 Output:

Red
Green
Blue

👉 With key & value:

$student = ["name" => "Aditya", "age" => 22];foreach ($student as $key => $value) {
echo $key . ": " . $value . "<br>";
}

🔹 5. break Statement

Used to stop the loop immediately.

✅ Example:

for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break;
}
echo $i . "<br>";
}

🔍 Output:

1 2 3 4

🔹 6. continue Statement

Skips the current iteration and moves to the next one.

✅ Example:

for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i . "<br>";
}

🔍 Output:

1 2 4 5

🧠 Quick Summary

Loop TypeUse Case
forFixed number of iterations
whileCondition-based looping
do-whileRun at least once
foreachArrays
breakStop loop
continueSkip iteration


AI Spiritual Tools & Interactive Experiences

Explore powerful AI-driven tools for daily guidance, spirituality, fun quizzes, and self-discovery.

Today’s Quote

Get inspiring daily quotes powered by AI to motivate and guide your day.

Explore Now

AI Tarot Card Reader

Reveal insights about your future, love, and career with AI tarot readings.

Read Tarot

Love Match Calculator

Check compatibility and love predictions using AI-based analysis.

Check Match

Fortune Cookie

Open an AI fortune cookie and receive wisdom, luck, and fun messages.

Open Cookie

Quiz Categories

Engage with knowledge-based and fun quizzes across multiple categories.

Start Quiz

Panchang Calendar

View daily Panchang, auspicious timings, tithi, nakshatra, and festivals.

View Panchang

Online Numerology

Discover your destiny number, life path, and numerology predictions.

Calculate Now

Spiritual Feeds

Stay connected with spiritual thoughts, mantras, and divine content.

View Feeds

Quiz Hub

Attempt trending quizzes on GK, spirituality, festivals, and more.

Explore Quizzes