practice questions for PHP Loops

🟢 Basic Level

1️⃣ Print Numbers

Print numbers from 1 to 10 using:

  • for
  • while

2️⃣ Even Numbers

Print all even numbers from 1 to 20


3️⃣ Sum of Numbers

Find the sum of numbers from 1 to 100

👉 Output: 5050


4️⃣ Reverse Counting

Print numbers from 10 to 1


5️⃣ Multiplication Table

Print table of 5

👉 Output:

5 x 1 = 5
5 x 2 = 10
...

🟡 Intermediate Level

6️⃣ Factorial Program

Find factorial of a number
👉 Example: 5! = 120


7️⃣ Fibonacci Series

Print first 10 Fibonacci numbers

👉 Output:

0 1 1 2 3 5 8 ...

8️⃣ Count Digits

Input: 12345
Output: 5 digits


9️⃣ Reverse Number

Input: 1234
Output: 4321


🔟 Prime Number Check

Check whether a number is prime or not


🟠 Array + foreach Practice

1️⃣1️⃣ Print Array Values

$fruits = ["Apple", "Banana", "Mango"];

Print all values using foreach


1️⃣2️⃣ Find Largest Number

$numbers = [10, 25, 5, 40, 15];

Find the largest number


1️⃣3️⃣ Sum of Array

Find total sum of array elements


1️⃣4️⃣ Count Even Numbers in Array

Count how many even numbers are in array


🔴 Advanced Level

1️⃣5️⃣ Pattern Printing (Star)

*
**
***
****
*****

1️⃣6️⃣ Reverse Pattern

*****
****
***
**
*

1️⃣7️⃣ Skip Number using continue

Print 1 to 10 but skip 5


1️⃣8️⃣ Stop Loop using break

Print numbers until 7 comes, then stop


1️⃣9️⃣ Nested Loop (Table 1–5)

Print multiplication tables from 1 to 5


2️⃣0️⃣ Login Attempt System (Real Use)

Allow user 3 attempts to enter correct password
If correct → “Login Successful”
Else → “Account Locked”


💡 Pro Teaching Tip (For Your Students)

Start with:

  1. Numbers → Logic building
  2. Patterns → Loop mastery
  3. Arrays → Real-world usage
  4. Mini projects → Confidence