w3resource

PHP for loop - Exercises, Practice, Solution


This resource offers a total of 190 PHP for loop problems for practice. It includes 38 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]

1. Number Sequence Display

Create a script that displays 1-2-3-4-5-6-7-8-9-10 on one line. There will be no hyphen(-) at starting and ending position.

Click me to see the solution

2. Sum Integers 0 to 30

Create a script using a for loop to add all the integers between 0 and 30 and display the total.

Click me to see the solution

3. Construct Incremental Star Pattern

Create a script to construct the following pattern, using nested for loop.

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

Click me to see the solution

4. Construct Symmetric Star Pattern

Create a script to construct the following pattern, using a nested for loop.

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

Click me to see the solution

5. Factorial Calculation

Write a program to calculate and print the factorial of a number using a for loop. The factorial of a number is the product of all integers up to and including that number, so the factorial of 4 is 4*3*2*1= 24.

Click me to see the solution

6. Two-Digit Decimal Combinations

Write a program which will give you all of the potential combinations of a two-digit decimal combination, printed in a comma delimited format :
Sample output :
00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,

Click me to see the solution

7. Count "r" Characters in a String

Write a program which will count the "r" characters in the text "w3resource".

Click me to see the solution

8. Create Multiplication Table with For Loops

Write a PHP script that creates the following table using for loops. Add cellpadding="3px" and cellspacing="0px" to the table tag.

1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30

Click me to see the solution

9. Chess Board Using Nested For Loops

Write a PHP script using nested for loop that creates a chess board as shown below.
Use table width="270px" and take 30px as cell height and width.

chess board

Click me to see the solution

10. Multiplication Table 10x10

Write a PHP script that creates the following table (use for loops).

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

Click me to see the solution

11. FizzBuzz Implementation

Write a PHP program which iterates the integers from 1 to 50. For multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

Click me to see the solution

12. Floyd Triangle Generation

Write a PHP program to generate and display the first n lines of a Floyd triangle. (use n=5 and n=11 rows).
According to Wikipedia Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:
Sample output for n = 5 :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Click me to see the solution

13. Alphabet Pattern 'A'

Write a PHP program to print alphabet pattern 'A'.
Expected Output:

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

Click me to see the solution

14. Alphabet Pattern 'B'

Write a PHP program to print alphabet pattern 'B'.
Expected Output:

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

Click me to see the solution

15. Alphabet Pattern 'C'

Write a PHP program to print alphabet pattern 'C'.
Expected Output:

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

Click me to see the solution

16. Alphabet Pattern 'D'

Write a PHP program to print alphabet pattern 'D'.
Expected Output:

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

Click me to see the solution

17. Alphabet Pattern 'E'

Write a PHP program to print alphabet pattern 'E'.
Expected Output:

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

Click me to see the solution

18. Alphabet Pattern 'F'

Write a PHP program to print alphabet pattern 'F'.
Expected Output:

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

Click me to see the solution

19. Alphabet Pattern 'G'

Write a PHP program to print alphabet pattern 'G'.
Expected Output:

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

Click me to see the solution

20. Alphabet Pattern 'H'

Write a PHP program to print alphabet pattern 'H'.
Expected Output:

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

Click me to see the solution

21. Alphabet Pattern 'I'

Write a PHP program to print alphabet pattern 'I'.
Expected Output:

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

Click me to see the solution

22. Alphabet Pattern 'J'

Write a PHP program to print alphabet pattern 'J'.
Expected Output:

   ***                                                      
    *                                                       
    *                                                       
    *                                                       
    *                                                       
  * *                                                       
   *  

Click me to see the solution

23. Alphabet Pattern 'K'

Write a PHP program to print alphabet pattern 'K'.
Expected Output:

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

Click me to see the solution

24. Alphabet Pattern 'L'

Write a PHP program to print alphabet pattern 'L'.
Expected Output:

 *                                                          
 *                                                          
 *                                                          
 *                                                          
 *                                                          
 *                                                          
 ***** 

Click me to see the solution

25. Alphabet Pattern 'M'

Write a PHP program to print alphabet pattern 'M'.
Expected Output:

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

Click me to see the solution

26. Alphabet Pattern 'N'

Write a PHP program to print alphabet pattern 'N'.
Expected Output:

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

Click me to see the solution

27. Alphabet Pattern 'O'

Write a PHP program to print alphabet pattern 'O'.
Expected Output:

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

Click me to see the solution

28. Alphabet Pattern 'P'

Write a PHP program to print alphabet pattern 'P'.
Expected Output:

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

Click me to see the solution

29. Alphabet Pattern 'Q'

Write a PHP program to print alphabet pattern 'Q'.
Expected Output:

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

Click me to see the solution

30. Alphabet Pattern 'R'

Write a PHP program to print alphabet pattern 'R'.
Expected Output:

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

Click me to see the solution

31. Alphabet Pattern 'S'

Write a PHP program to print alphabet pattern 'S'.
Expected Output:

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

Click me to see the solution

32. Alphabet Pattern 'T'

Write a PHP program to print alphabet pattern 'T'.
Expected Output:

 *****                                                      
   *                                                        
   *                                                        
   *                                                        
   *                                                        
   *  

Click me to see the solution

33. Alphabet Pattern 'U'

Write a PHP program to print alphabet pattern 'U'.
Expected Output:

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

Click me to see the solution

34. Alphabet Pattern 'V'

Write a PHP program to print alphabet pattern 'V'.
Expected Output:

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

Click me to see the solution

35. Alphabet Pattern 'W'

Write a PHP program to print alphabet pattern 'W'.
Expected Output:

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

Click me to see the solution

36. Alphabet Pattern 'X'

Write a PHP program to print alphabet pattern 'X'.
Expected Output:

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

Click me to see the solution

37. Alphabet Pattern 'Y'

Write a PHP program to print alphabet pattern 'Y'.
Expected Output:

 *   *                                                      
 *   *                                                      
  * *                                                       
   *                                                        
   *                                                        
   *                                                        
   *

Click me to see the solution

38. Alphabet Pattern 'Z'

Write a PHP program to print alphabet pattern 'Z'.
Expected Output:

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

Click me to see the solution

PHP Code Editor:



More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.