PHP for loop: Exercise-10 with Solution
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 |
Sample Solution:
PHP Code:
Sample Output:
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 |
View the output in the browser
Flowchart:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a program which will count the "r" characters in the text "w3resource".
Next: Write a PHP script using nested for loop that creates a chess board as shown below.
PHP: Tips of the Day
PHP: Creating default object from empty value in PHP?
Your new environment may have E_STRICT warnings enabled in error_reporting for PHP versions = 5.4. That error is triggered when $res is NULL or not yet initialized:
$res = NULL; $res->success = false; // Warning: Creating default object from empty value
PHP will report a different error message if $res is already initialized to some value but is not an object:
$res = 33; $res->success = false; // Warning: Attempt to assign property of non-object
In order to comply with E_STRICT standards prior to PHP 5.4, or the normal E_WARNING error level in PHP >= 5.4, assuming you are trying to create a generic object and assign the property success, you need to declare $res as an object of stdClass in the global namespace:
$res = new \stdClass[]; $res->success = false;
Ref : //bit.ly/3mPEvSn
How to create table in php for loop?
", [[$col + $row] % 5] + 1, " | "; } echo "