PHP for loop - Exercises, Practice, Solution
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
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
3. Create a script to construct the following pattern, using nested for loop. Go to the editor
* * * * * * * * * * * * * * *
4. Create a script to construct the following pattern, using a nested for loop. Go to the editor
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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
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,
7. Write a program which will count the "r" characters in the text "w3resource". Go to the editor
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 |
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.
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 |
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
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
13. Write a PHP program to print alphabet pattern 'A'. Go to the editor
Expected Output:
*** * * * * ***** * * * * * * * *
14. Write a PHP program to print alphabet pattern 'B'. Go to the editor
Expected Output:
**** * * * * **** * * * * ****
15. Write a PHP program to print alphabet pattern 'C'. Go to the editor
Expected Output:
*** * * * * * * * ***
16. Write a PHP program to print alphabet pattern 'D'. Go to the editor
Expected Output:
**** * * * * * * * * * * ****
17. Write a PHP program to print alphabet pattern 'E'. Go to the editor
Expected Output:
***** * * **** * * *****
18. Write a PHP program to print alphabet pattern 'F'. Go to the editor
Expected Output:
***** * * **** * * *
19. Write a PHP program to print alphabet pattern 'G'. Go to the editor
Expected Output:
*** * * * * *** * * * * ***
20. Write a PHP program to print alphabet pattern 'H'. Go to the editor
Expected Output:
* * * * * * ***** * * * * * *
21. Write a PHP program to print alphabet pattern 'I'. Go to the editor
Expected Output:
***** * * * * * *****
22. Write a PHP program to print alphabet pattern 'J'. Go to the editor
Expected Output:
*** * * * * * * *
23. Write a PHP program to print alphabet pattern 'K'. Go to the editor
Expected Output:
* * * * * * ** * * * * * * * *
24. Write a PHP program to print alphabet pattern 'L'. Go to the editor
Expected Output:
* * * * * * *****
25. Write a PHP program to print alphabet pattern 'M'. Go to the editor
Expected Output:
* * * * ** ** * * * * * * * * *
26. Write a PHP program to print alphabet pattern 'N'. Go to the editor
Expected Output:
* * * * ** * * * * * ** * * * *
27. Write a PHP program to print alphabet pattern 'O'. Go to the editor
Expected Output:
*** * * * * * * * * * * ***
28. Write a PHP program to print alphabet pattern 'P'. Go to the editor
Expected Output:
**** * * * * **** * * *
29. Write a PHP program to print alphabet pattern 'Q'. Go to the editor
Expected Output:
*** * * * * * * * * * * * ** *
30. Write a PHP program to print alphabet pattern 'R'. Go to the editor
Expected Output:
**** * * * * **** * * * * * *
31. Write a PHP program to print alphabet pattern 'S'. Go to the editor
Expected Output:
**** * * *** * * ****
32. Write a PHP program to print alphabet pattern 'T'. Go to the editor
Expected Output:
***** * * * * *
33. Write a PHP program to print alphabet pattern 'U'. Go to the editor
Expected Output:
* * * * * * * * * * * * ***
34. Write a PHP program to print alphabet pattern 'V'. Go to the editor
Expected Output:
* * * * * * * * * * * * *
35. Write a PHP program to print alphabet pattern 'W'. Go to the editor
Expected Output:
* * * * * * * * * * * * * * * *
36. Write a PHP program to print alphabet pattern 'X'. Go to the editor
Expected Output:
* * * * * * * * * * * * *
37. Write a PHP program to print alphabet pattern 'Y'. Go to the editor
Expected Output:
* * * * * * * * * *
38. Write a PHP program to print alphabet pattern 'Z'. Go to the editor
Expected Output:
******* * * * * * *******
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.
PHP: Tips of the Day
In PHP, there are two versions of logical AND and OR operators.
Operator | True if |
---|---|
$a and $b | Both $a and $b are true |
$a && $b | Both $a and $b are true |
$a or $b | Either $a or $b is true |
$a || $b | Either $a or $b is true |
Note that the && and || opererators have higher precedence than and and or. See table below:
Evaluation | Result of $e | Evaluated as |
---|---|---|
$e = false || true | True | $e = (false || true) |
$e = false or true | False | ($e = false) or true |
Because of this it's safer to use && and || instead of and and or.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework