How to compare a variable several times with a =

1

What the title says, I'm doing a project and in a specific part I want to add 1 to the value of a variable, I hope that with the code they understand my doubt

$resultado = $_POST['resultado'];

Suppose that the value sent by the form is 10. And I have a scale of results ranging from -18 to 18, that is.

-10 a -18 = N
-1 a -9 = LN
 0 = S
 1 a 9 = LP
 10 a 18 = P

Then ...

if ($resultado == 0) {
  $S++;
} elseif ($resultado >= 1) {
  $LP++;
} elseif ($resultado >= 10) {
  $P++;
} elseif ($resultado <= -1) {
  $LN++;
} elseif ($resultado <= -10) {
  $N++;
}

When the result is from 1 to 9 the variable $LP increases, the problem is that when the result goes from 10 to 18 instead of increasing the variable $P increases the variable $LP and the same happens to me with the variables $LN and $N .

    
asked by David 26.04.2018 в 21:01
source

4 answers

3

You have to validate the value of your result by verifying that it is among the ranges you have defined:

if ($resultado == 0) {
  $S++;
} elseif ($resultado >= 1 && $resultado <= 9) {
  $LP++;
} elseif ($resultado >= 10 && $resultado <= 18) {
  $P++;
} elseif ($resultado >= -9 && $resultado <= -1) {
  $LN++;
} elseif ($resultado >= -18 && $resultado <= -10) {
  $N++;
}

I hope it serves you.

    
answered by 26.04.2018 в 21:18
2

The problem that you are having, is that in this case, the order of if if it alters the result.

Notice how you have it now:

if ($resultado == 0){
    $S++;
}elseif ($resultado >= 1){
    $LP++;
}elseif ($resultado >= 10){
    $P++;
}elseif ($resultado <= -1){
    $LN++;
}elseif ($resultado <= -10){
    $N++;
}

If result is 3, enter the first if . If the result is 15, it is greater than one, then enter the first if . If the result is -3, enter the third.

It is important to keep in mind that if you are going to do something like that, the order of revision of the parameters is very important.

Try something like this:

if ($resultado == 0){
    $S++;
}elseif ($resultado <= -10){
    $N++;
}elseif ($resultado <= -1){
    $LN++;
}elseif ($resultado >= 10){
    $P++;
}elseif ($resultado >= 1){
    $LP++;
}
    
answered by 26.04.2018 в 21:47
1

If you really want to make this comparison:

-10 a -18 = N
-1  a -9  = LN
 0  =       S
 1  a  9  = LP
 10 a 18  = P

An interesting option could be the following:

  • You create a function to which you pass: the current value, the values of the range, and the value of each variable by reference .
  • The function will create an array of values with the range using range and will search if within the array is the value of $v , if it finds it, it will increase the value of each case by reference.

The code would be more or less like this:

SEE DEMO IN REXTESTER

/*Probar una serie de valores*/

$arrPruebas=array(-11,-2,0,7,12);
foreach($arrPruebas as $v){

    /*Inicializar variables para fines de prueba*/
    $S=0;$LP=0;$P=0;$LN=0;$N=0;  

    /*Comparaciones*/
    inRange($v,-10,-18,$N);
    inRange($v,-1,-9,$LN);
    inRange($v,0,0,$S);
    inRange($v,1,9,$LP);
    inRange($v,10,18,$P);

    /*Salida*/
    echo "Prueba siendo $"."v=$v".PHP_EOL.PHP_EOL;
    echo "\tS\tLP\tP\tLN\tN".PHP_EOL;
    echo "\t$S\t$LP\t$P\t$LN\t$N".PHP_EOL.PHP_EOL."***".PHP_EOL;   

}

/*Función para comparar si está en el rango*/

function inRange($v, $min, $max, &$ref){
    $val=range($min,$max);
    if ( in_array ($v, $val) ) 
        {
            $ref++;
        }
}

Test results by case:

- Prueba siendo $v=-11

    S   LP  P   LN  N
    0   0   0   0   1
 - Prueba siendo $v=-2

    S   LP  P   LN  N
    0   0   0   1   0
 - Prueba siendo $v=0

    S   LP  P   LN  N
    1   0   0   0   0
 - Prueba siendo $v=7

    S   LP  P   LN  N
    0   1   0   0   0
 - Prueba siendo $v=12

    S   LP  P   LN  N
    0   0   1   0   0
    
answered by 27.04.2018 в 01:13
0

One more alternative

#!/usr/bin/env php
<?php
$S=0;$LP=0;$P=0;$LN=0;$N=0;
echo "$S;$LP;$P;$LN;$N;".PHP_EOL;

$resultado = 1;

function between($n, $a, $b) {
    return ($n-$a)*($n-$b) <= 0;
}

if ($resultado==0) :
  $S++;
elseif (between($resultado, 1, 9)) :
  $LP++;
elseif (between($resultado, 10, 18)) :
  $P++;
elseif (between($resultado, -9, -1)) :
  $LN++;
elseif (between($resultado, -18, -10)) :
  $N++;
endif;

echo "$S;$LP;$P;$LN;$N;".PHP_EOL;

implementation of betweeen based on link

    
answered by 26.04.2018 в 22:14