PHP for loop - Exercises, Practice, Solution
PHP for loop [38 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
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.
2. Create a script using a for loop to add all the integers between 0 and 30 and display the total.
3. Create a script to construct the following pattern, using nested for loop.
* * * * * * * * * * * * * * *
4. Create a script to construct the following pattern, using a nested for loop.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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.
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 :
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".
8. 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 |
9. 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.
10. 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 |
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".
12. 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
13. Write a PHP program to print alphabet pattern 'A'.
Expected Output:
*** * * * * ***** * * * * * * * *
14. Write a PHP program to print alphabet pattern 'B'.
Expected Output:
**** * * * * **** * * * * ****
15. Write a PHP program to print alphabet pattern 'C'.
Expected Output:
*** * * * * * * * ***
16. Write a PHP program to print alphabet pattern 'D'.
Expected Output:
**** * * * * * * * * * * ****
17. Write a PHP program to print alphabet pattern 'E'.
Expected Output:
***** * * **** * * *****
18. Write a PHP program to print alphabet pattern 'F'.
Expected Output:
***** * * **** * * *
19. Write a PHP program to print alphabet pattern 'G'.
Expected Output:
*** * * * * *** * * * * ***
20. Write a PHP program to print alphabet pattern 'H'.
Expected Output:
* * * * * * ***** * * * * * *
21. Write a PHP program to print alphabet pattern 'I'.
Expected Output:
***** * * * * * *****
22. Write a PHP program to print alphabet pattern 'J'.
Expected Output:
*** * * * * * * *
23. Write a PHP program to print alphabet pattern 'K'.
Expected Output:
* * * * * * ** * * * * * * * *
24. Write a PHP program to print alphabet pattern 'L'.
Expected Output:
* * * * * * *****
25. Write a PHP program to print alphabet pattern 'M'.
Expected Output:
* * * * ** ** * * * * * * * * *
26. Write a PHP program to print alphabet pattern 'N'.
Expected Output:
* * * * ** * * * * * ** * * * *
27. Write a PHP program to print alphabet pattern 'O'.
Expected Output:
*** * * * * * * * * * * ***
28. Write a PHP program to print alphabet pattern 'P'.
Expected Output:
**** * * * * **** * * *
29. Write a PHP program to print alphabet pattern 'Q'.
Expected Output:
*** * * * * * * * * * * * ** *
30. Write a PHP program to print alphabet pattern 'R'.
Expected Output:
**** * * * * **** * * * * * *
31. Write a PHP program to print alphabet pattern 'S'.
Expected Output:
**** * * *** * * ****
32. Write a PHP program to print alphabet pattern 'T'.
Expected Output:
***** * * * * *
33. Write a PHP program to print alphabet pattern 'U'.
Expected Output:
* * * * * * * * * * * * ***
34. Write a PHP program to print alphabet pattern 'V'.
Expected Output:
* * * * * * * * * * * * *
35. Write a PHP program to print alphabet pattern 'W'.
Expected Output:
* * * * * * * * * * * * * * * *
36. Write a PHP program to print alphabet pattern 'X'.
Expected Output:
* * * * * * * * * * * * *
37. Write a PHP program to print alphabet pattern 'Y'.
Expected Output:
* * * * * * * * * *
38. Write a PHP program to print alphabet pattern 'Z'.
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
Getting all defined constants
To get all defined constants including those created by PHP use the get_defined_constants function:
Example:
<?php $constants = get_defined_constants(); var_dump($constants); // pretty large list
Output:
array(2250) { ["E_ERROR"]=> int(1) ["E_RECOVERABLE_ERROR"]=> int(4096) ["E_WARNING"]=> ..... ..... resource(1) of type (stream) ["STDOUT"]=> resource(2) of type (stream) ["STDERR"]=> resource(3) of type (stream) }
To get only those constants that were defined by your app call the function at the beginning and at the end of your script (normally after the bootstrap process):
<?php $constants = get_defined_constants(); define("HELLO", "hello"); define("WORLD", "world"); $new_constants = get_defined_constants(); $myconstants = array_diff_assoc($new_constants, $constants); var_export($myconstants);
Output:
array ( 'HELLO' => 'hello', 'WORLD' => 'world', )
It's sometimes useful for debugging
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook