While PHP does not exceed count

1

How do I pro-identify that will exceed the count set on the form and pause before?

FORM:

<form method="get" action="exercico04.php">
Inicio: <input type="number" name="inicio" value="1" max="100" min="1"/><br/>
Final: <input type="number" name="final" value="1" max="100" min="1"/><br/>
Incremento:
<select name="incremento">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>
<br>
<input type="submit" value="Contar" class="botao"/>

PHP:

$inis = isset($_GET["inicio"])?$_GET["inicio"]:0;
    $finis = isset($_GET["final"])?$_GET["final"]:0;
    $incris = isset($_GET["incremento"])?$_GET["incremento"]:0;

    if($inis < $finis){
        echo $inis . "  ";
        while($inis < $finis) {
            $inis+=$incris;
        echo $inis."  ";
    }
}else if($inis > $finis){
    echo $inis . ",";
    while($inis > $finis) {
            $inis-=$incris;
        echo $inis ."  ";
    }
}

In the case, for example, if I choose HOME 7, FINAL 45 and INCREMENT 4, it will count as follows: 7 11 15 19 23 27 31 35 39 43 47

Only in case, I want it to stop before reaching 45, which would be in case no 43. How do I do this?

    
asked by anonymous 11.05.2017 / 21:24

3 answers

5

There are some errors in your code, such as if($inis $finis) , this condition is invalid, another error is the condition of while where while($inis > $finis) { if start is getting 7 and end 45 will never enter the loop, last , you are decrementing the start and not increasing, see an example with the corrections:

$inis = isset($_GET["inicio"])?$_GET["inicio"]:0;
$finis = isset($_GET["final"])?$_GET["final"]:0;
$incris = isset($_GET["incremento"])?$_GET["incremento"]:0;

if($inis != 0 && $finis != 0 && $incris != 0){
   /*Verifica se o usuário não inverteu inicio e fim, se sim troca
   *Por exemplo: $inis = 45; $finis = 7;
   *Neste caso será necessário inverter as variáveis
   *
   */
   if($inis > $finis){
      $aux = $inis;//armazena o valor original de $inis na variável $aux = 45
      $inis = $finis; //$inis = 7
      $finis = $aux; //Agora recebe valor de $aux, ou seja $finis = 45
      //Depois dessa troca os valores de início e fim estarão corretos
      //$inis = 7;
      //$finis = 45;
   }

   while($inis < $finis) {
     echo $inis ."  "; // imprime o primeiro valor
     $inis+=$incris;//incrementa
     if($inis > $finis) {//se após incrementar $inis > $finis
       $inis = $finis;
       echo $inis ."  ";//imprime o $finis
     }  
   }
}

IDEONE EXAMPLE

    
11.05.2017 / 21:35
4

Using a for

if($_GET["inicio"] != 0 && $_GET["final"] != 0 && $_GET["incremento"] != 0){
   $inis=$_GET["inicio"];
   $finis=$_GET["final"];
   $incris=$_GET["incremento"];


    for ($x = $inis; $x < $finis; $x=($x+$incris)) {

        if ($x == $inis){
            echo $inis ."  ";
        }else{
            $inis+=$incris;
            echo $inis ."  ";
        }
    }
}
  

The FOR repeat structure is used to execute a set of commands for a set number of times. For this operator, an initial situation, a condition, and an action to be performed with each repetition are passed.

     

In general we inform a variable that serves as a counter of repetitions, with its initial value, a condition to be met so that each repetition is executed and an increment to the counter.

FOR operator syntax

for(valor inicial; condição; incremento)
{
  //comandos
}

Taking advantage of the fabulous idea of our friend abfurlan

"Verify that the user did not reverse start and end, if yes change"

It would look like this:

$inis = isset($_GET["inicio"])?$_GET["inicio"]:0;
$finis = isset($_GET["final"])?$_GET["final"]:0;
$incris = isset($_GET["incremento"])?$_GET["incremento"]:0;

if($inis != 0 && $finis != 0 && $incris != 0){
   if($inis > $finis){
      $aux = $inis;
      $inis = $finis;
      $finis = $aux;

   }

        for ($x = $inis; $x < $finis; $x=($x+$incris)) {

            if ($x == $inis){
                echo $inis ."  ";
            }else{
                $inis+=$incris;
                echo $inis ."  ";
            }
        }  

}
    
11.05.2017 / 22:23
3

A simpler solution to doing what you want is to use range() , which is native .

That's all you need:

// Trata o input:
$inicio = $_GET['inicio'] ?? 0;
$inicio = is_numeric($inicio) ? $inicio : 0;

$final = $_GET['final'] ?? 0;
$final = is_numeric($final) ? $final : 0;

$incremento = $_GET['incremento'] ?? 1;
$incremento = is_numeric($incremento) && $incremento != 0 ? $incremento : 1;

// Faz o mesmo que o 'while':
echo implode(' ', range($inicio, $final, $incremento));

Try this here

The range() will cause it to go from $inicio to $fim , if set a $incremento it will "jump", it can not be 0 . It returns a array , for example a var_export(range(7, 45, 4)); results in:

array ( 0 => 7, 1 => 11, 2 => 15, 3 => 19, 4 => 23, 5 => 27, 6 => 31, 7 => 35, 8 => 39, 9 => 43 )

That way, we have 7 to 43 , as you wish.

The implode() is made to join the values, so we put everything together with a (space) between them.

In this way the result we have of implode(' ', range(7, 45, 4)) is just:

7 11 15 19 23 27 31 35 39 43

See the documentation of implode() by clicking here and a documentation of range() here . It should be noted that range() also works for letters, range('a', 'z') will result in the alphabet. ;)

    
12.05.2017 / 00:01