[php] arrays and loops

Margaret Iaquinto iaquinto at ozemail.com.au
Sat Oct 30 15:40:31 EST 2010


  I can gather data into an array by using a form. I can spill the 
contents of that array by using a loop.

In the code snippet below, the user enters the name of a programming 
language.

snippet from the form:

p><input type = "text" name = "languages[]" >Add a language</p>
<p><input type = "text" name = "languages[]" > Add a language</p>
<p><input type = "text" name = "languages[]" > Add a language</p>
<p><input type = "text" name = "languages[]" > Add a language</p>

The php code snippet looks like this:

echo "Display the data  -  using a REPEAT UNTIL loop";
echo "<br>";
$position = 0;
do {
     echo "$languages[$position]";
     $position += 1;
     echo "<br>";
}
while ($position < count($languages));

And the code works.

The form limits the number of languages to four, in the code above.

Is there a way to use a loop or some kind of syntax to ask the user to 
keep entering data until a sentinel is entered?
In VB, I can use an InputBox within a loop. When the user types in, say 
-1, this ends the loop and the array stops being filled.

But how do I do that in PHP?

Thanks

Maggie


More information about the php mailing list