Algorithm for reading multiple numbers in PHP

Asked

Viewed 266 times

0

I have a question about how to do the following exercise in PHP using HTML forms:

Make an algorithm that reads a sequence of integer numbers. If the user type a negative number the algorithm should return the average of the numbers typed (without the negative value).

In another programming language, I would create a loop of repetition with the stop condition being the value read be a negative number, but I do not know how to do this for a POST request, which was requested by the teacher. If you could help me, I’d be most grateful.

1 answer

2


Good afternoon, my friend. My suggestion is to read the entire sequence and then split the sequence.

$sequencia = explode(",",$_POST["sequencia"]);
$soma = 0;
foreach ($sequencia as &$valor) {
  if ( $valor > 0 )
     $soma += $valor
  else break; }

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.