Can I use php to read values from one input and store it in array?
I want to read multi integers from the user and calculate the total of them if the user entered ZERO!
For example, I add one input text and one submit:
<form method="GET">
enter numbers : <input type = "text" name="txt">
<input type = "submit" value = "Calculate">
</form>
then use php to read them !!
<?php
$n = @$_GET[txt];
$i=0;
while ($n!=0)
{
$ary[$i]=$n;
$i++;
}
if ($n==0)
echo @count($ary);
?>
it doesn't work .. what should I do here? There is the same problem in java, but there I can use scanner. Can any one help to solve it with php?
I think this is what you're asking for:
// Get the value of the input box, note that I put `txt` in quotes
// so I didn't need the `@`
$txt = $_GET ['txt'];
// `explode` will create an array from a string using the first
// parameter as a delimiter
$ary = explode (' ', $txt);
// Search the array for the ZERO value
if (array_search(0, $ary) !== false) {
// Output the sum of the array if the ZERO value is found
echo (array_sum ($ary));
}
A note: You should almost never use the @ operator in PHP, if you do then you're likely doing some incorrect.