w3resource logo


php exercises

PHP for loop - Exercises, Practice, Solution

Secondary Nav

PHP for loop [38 exercises with solution]

1. 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. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

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

Click me to see the solution

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

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

Click me to see the solution

5. 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. Go to the editor

Click me to see the solution

6. Write a program which will give you all of the potential combinations of a two-digit decimal combination, printed in a comma delimited format : Go to the editor
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. Write a program which will count the "r" characters in the text "w3resource". Go to the editor

Click me to see the solution

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

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. Write a PHP script using nested for loop that creates a chess board as shown below. Go to the editor
Use table width="270px" and take 30px as cell height and width.

chess board

Click me to see the solution

10. Write a PHP script that creates the following table (use for loops). Go to the editor

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. 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". Go to the editor

Click me to see the solution

12. Write a PHP program to generate and display the first n lines of a Floyd triangle. (use n=5 and n=11 rows). Go to the editor
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. Write a PHP program to print alphabet pattern 'A'. Go to the editor
Expected Output:

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

Click me to see the solution

14. Write a PHP program to print alphabet pattern 'B'. Go to the editor
Expected Output:

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

Click me to see the solution

15. Write a PHP program to print alphabet pattern 'C'. Go to the editor
Expected Output:

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

Click me to see the solution

16. Write a PHP program to print alphabet pattern 'D'. Go to the editor
Expected Output:

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

Click me to see the solution

17. Write a PHP program to print alphabet pattern 'E'. Go to the editor
Expected Output:

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

Click me to see the solution

18. Write a PHP program to print alphabet pattern 'F'. Go to the editor
Expected Output:

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

Click me to see the solution

19. Write a PHP program to print alphabet pattern 'G'. Go to the editor
Expected Output:

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

Click me to see the solution

20. Write a PHP program to print alphabet pattern 'H'. Go to the editor
Expected Output:

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

Click me to see the solution

21. Write a PHP program to print alphabet pattern 'I'. Go to the editor
Expected Output:

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

Click me to see the solution

22. Write a PHP program to print alphabet pattern 'J'. Go to the editor
Expected Output:

   ***                                                      
    *                                                       
    *                                                       
    *                                                       
    *                                                       
  * *                                                       
   *  

Click me to see the solution

23. Write a PHP program to print alphabet pattern 'K'. Go to the editor
Expected Output:

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

Click me to see the solution

24. Write a PHP program to print alphabet pattern 'L'. Go to the editor
Expected Output:

 *                                                          
 *                                                          
 *                                                          
 *                                                          
 *                                                          
 *                                                          
 ***** 

Click me to see the solution

25. Write a PHP program to print alphabet pattern 'M'. Go to the editor
Expected Output:

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

Click me to see the solution

26. Write a PHP program to print alphabet pattern 'N'. Go to the editor
Expected Output:

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

Click me to see the solution

27. Write a PHP program to print alphabet pattern 'O'. Go to the editor
Expected Output:

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

Click me to see the solution

28. Write a PHP program to print alphabet pattern 'P'. Go to the editor
Expected Output:

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

Click me to see the solution

29. Write a PHP program to print alphabet pattern 'Q'. Go to the editor
Expected Output:

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

Click me to see the solution

30. Write a PHP program to print alphabet pattern 'R'. Go to the editor
Expected Output:

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

Click me to see the solution

31. Write a PHP program to print alphabet pattern 'S'. Go to the editor
Expected Output:

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

Click me to see the solution

32. Write a PHP program to print alphabet pattern 'T'. Go to the editor
Expected Output:

 *****                                                      
   *                                                        
   *                                                        
   *                                                        
   *                                                        
   *  

Click me to see the solution

33. Write a PHP program to print alphabet pattern 'U'. Go to the editor
Expected Output:

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

Click me to see the solution

34. Write a PHP program to print alphabet pattern 'V'. Go to the editor
Expected Output:

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

Click me to see the solution

35. Write a PHP program to print alphabet pattern 'W'. Go to the editor
Expected Output:

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

Click me to see the solution

36. Write a PHP program to print alphabet pattern 'X'. Go to the editor
Expected Output:

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

Click me to see the solution

37. Write a PHP program to print alphabet pattern 'Y'. Go to the editor
Expected Output:

 *   *                                                      
 *   *                                                      
  * *                                                       
   *                                                        
   *                                                        
   *                                                        
   *

Click me to see the solution

38. Write a PHP program to print alphabet pattern 'Z'. Go to the editor
Expected Output:

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

Click me to see the solution

PHP Practice online:

<?php

?>

Go Top

More to Come !



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number