Table of 2 using for loop in php w3schools

PHP for Loop


The for loop - Loops through a block of code a specified number of times.


The PHP for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for (init counter; test counter; increment counter) {
  code to be executed for each iteration;
}

Parameters:

  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value

Examples

The example below displays the numbers from 0 to 10:

Example

for ($x = 0; $x <= 10; $x++) {
  echo "The number is: $x
";
}
?>

Try it Yourself »

Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 10; - Continue the loop as long as $x is less than or equal to 10
  • $x++ - Increase the loop counter value by 1 for each iteration

This example counts to 100 by tens:

Example

for ($x = 0; $x <= 100; $x+=10) {
  echo "The number is: $x
";
}
?>

Try it Yourself »

Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 100; - Continue the loop as long as $x is less than or equal to 100
  • $x+=10 - Increase the loop counter value by 10 for each iteration


PHP Exercises



PHP foreach Loop


The foreach loop - Loops through a block of code for each element in an array.


The PHP foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Syntax

foreach ($array as$value) {
  code to be executed;
}

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

Examples

The following example will output the values of the given array ($colors):

Example

$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value
";
}
?>

Try it Yourself »

The following example will output both the keys and the values of the given array ($age):

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {
  echo "$x = $val
";
}
?>

Try it Yourself »

You will learn more about arrays in the PHP Arrays chapter.



PHP program to print table of a number:

The below program prints table of a number using a loop. The PHP echo statement is used to output the result on the screen.
Example

DOCTYPE html>
<html>
<body>
 
php    
$num = 9;  
for($i=1; $i<=10; $i++)   
{
$product = $i*$num;
echo "$num * $i = $product" ;   
echo '
'
; } ?>   body> html>

Output

9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

Table of 2 using for loop in php w3schools

Please Share

How do you use a loop in a table?

Algorithm. Step 1: Enter a number to print table at runtime. Step 2: Read that number from keyboard. Step 3: Using for loop print number*I 10 times. // for(i=1; i<=10; i++) Step 4: Print num*I 10 times where i=0 to 10.

How do you create a loop in a table?

var table = document. createElement('table'), tr, td, i; for (i = 0; i < 220; i++) { if (i % 22 == 0) { // every 22nd cell (including the first) tr = table. appendChild(document. createElement('tr')); // add a new row } td = tr.

How can I print 1 to 10 numbers in PHP?

We can print numbers from 1 to 10 by using for loop. You can easily extend this program to print any numbers from starting from any value and ending on any value. The echo command will print the value of $i to the screen. In the next line we have used the same echo command to print one html line break.

How fetch data from for loop in PHP?

Follow the steps to fetch data from the Database in PHP PDO:.
Create Database: Create a database using XAMPP, the database is named “geeksforgeeks” here. ... .
Create Table: Create a table named “fetch_record” with 2 columns to store the data. ... .
Create Table Structure: The table “fetch_record” contains 2 fields..