Input and output using php loop

I want to create limit input tag with php for loop an get inputs values. My sample code is this:


   

Is my code correct? How can I get input values?

asked Oct 22, 2014 at 12:33

user3246727user3246727

1011 gold badge3 silver badges13 bronze badges

3

You can try my script.




The value of the text field is:

was not set.

answered Oct 22, 2014 at 12:44

Input and output using php loop

vaso123vaso123

12.3k4 gold badges32 silver badges63 bronze badges

Looks alright but I would use an array as the input name. For example:


   

That way in the back end you can just loop through the number array like so.

foreach ($_POST['number'] as $key => $value) {
    // Do stuff
}

answered Oct 22, 2014 at 12:37

David JonesDavid Jones

4,2456 gold badges26 silver badges51 bronze badges

0

Your code should render 10 html input fields with name 1, 2, 3, ... 10 correctly

To get input values, wrap your input fields in a form element with an action pointing to the php script in which you want to read the values (e.g. action="myscript.php").

(You should add a input type="submit" to have a way to submit the form. I assume you know HTML good enough to create a simple form.)

The script invoked by submitting the form (e.g. myscript.php) will now be able to read the values using the $_GET array. See http://php.net/manual/de/reserved.variables.get.php

You could print the values like so:


Edit: As @David Jones mentioned it would be better to use an array as input name

answered Oct 22, 2014 at 12:42

Sven TschuiSven Tschui

1,2777 silver badges19 bronze badges

A Loop is an Iterative Control Structure that involves executing the same number of code a number of times until a certain condition is met.

PHP For Loop

The above code outputs “21 is greater than 7” For loops For… loops execute the block of code a specifiednumber of times. There are basically two types of for loops;

  • for
  • for… each.

Let’s now look at them separately. For loop It has the following basic syntax

HERE,

  • “for…{…}” is the loop block
  • initialize” usually an integer; it is used to set the counter’s initial value.
  • “condition” the condition that is evaluated for each php execution. If it evaluates to true, then execution of the for… loop continues. If it evaluates to false, the execution of the for… loop is terminated.
  • “increment” is used to increment the initial value of counter integer.

How it works

The flowchart shown below illustrates how for loop in php works

Input and output using php loop

How to code

The code below uses the “for… loop” to print values of multiplying 10 by 0 through to 10

";
}

?>

Output:

The product of 10 x 0 is 0 
The product of 10 x 1 is 10 
The product of 10 x 2 is 20 
The product of 10 x 3 is 30 
The product of 10 x 4 is 40 
The product of 10 x 5 is 50 
The product of 10 x 6 is 60 
The product of 10 x 7 is 70 
The product of 10 x 8 is 80 
The product of 10 x 9 is 90 

PHP For Each loop

The php foreach loop is used to iterate through array values. It has the following basic syntax

HERE,

  • “foreach(…){…}” is the foreach php loop block code
  • “$array_data” is the array variable to be looped through
  • “$array_value “ is the temporary variable that holds the current array item values.
  • “block of code…” is the piece of code that operates on the array values

How it works The flowchart shown below illustrates how the for… each… loop works

Input and output using php loop

Practical examples

The code below uses for… each loop to read and print the elements of an array.

";

}

?>

Output:

Lion
Wolf
Dog
Leopard
Tiger

Let’s look at another example that loops through an associative array.

An associative array uses alphanumeric words for access keys.

 "Female", "John" => "Male", "Mirriam" => "Female");

foreach($persons as $key => $value){

echo "$key is $value"."
"; } ?>

The names have been used as array keys and gender as the values.

Output:

Mary is Female
John is Male
Mirriam is Female

PHP While loop

They are used to execute a block of code a repeatedly until the set condition gets satisfied

When to use while loops

  • While loops are used to execute a block of code until a certain condition becomes true.
  • You can use a while loop to read records returned from a database query.

Types of while loops

  • Do… while – executes the block of code at least once before evaluating the condition
  • While… – checks the condition first. If it evaluates to true, the block of code is executed as long as the condition is true. If it evaluates to false, the execution of the while loop is terminated.

While loop

It has the following syntax

HERE,

  • “while(…){…}” is the while loop block code
  • “condition” is the condition to be evaluated by the while loop
  • “block of code…” is the code to be executed if the condition gets satisfied

How it works

The flow chart shown below illustrates how the while… loop works

Input and output using php loop

Practical example

The code below uses the while… loop to print numbers 1 to 5.

";

$i++;

}

?>

Output:

1
2
3
4
5

PHP Do While

The difference between While… loop and Do… while loop is do… while is executed at-least once before the condition is evaluated.

Let’s now look at the basic syntax of a do… while loop

while(condition);

HERE,

  • “do{…} while(…)” is the do… while loop block code
  • “condition” is the condition to be evaluated by the while loop
  • “block of code…” is the code that is executed at least once by the do… while loop

How it works

The flow chart shown below illustrates how the while… loop works

Input and output using php loop

Practical example

We are now going to modify the while… loop example and implement it using the do… while loop and set the counter initial value to 9.

The code below implements the above modified example

";

}

while($i < 9);

?>

The above code outputs:

9

Note the above example outputs 9 only.

This is because the do… while loop is executed at least once even if the set condition evaluates to false.

Summary

  • The for… loop is used to execute a block of a specified number of times
  • The foreach… loop is used to loop through arrays
  • While… loop is used to execute a block of code as long as the set condition is made to be false
  • The do… while loop is used to execute the block of code at least once then the rest of the execution is dependent on the evaluation of the set condition

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 is PHP looping done?

In PHP, we have the following loop types:.
while - loops through a block of code as long as the specified condition is true..
do... ... .
for - loops through a block of code a specified number of times..
foreach - loops through a block of code for each element in an array..

Which loop is best in PHP?

For-Each loop is best when you have to iterate over collections.

What do you mean by PHP explain its loops with example?

Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types. for − loops through a block of code a specified number of times. while − loops through a block of code if and as long as a specified condition is true.