Detect the value change of a php variable

1

I have a question:

How is it possible to know when a variable in php changes value in a while cycle?

I have a query that I make to a BD in which it brings me several data, among them it brings me the number of days elapsed.

Then I throw the data through a while loop:

while ($row_h=mysqli_fetch_array($resultado4)){

    $cel_b = $letter_h.$cel_hrango;
    //$row_h['momento'] es el valor de la consulta de días puede tener valores 55,55,55,55,55,56,56,56,56,56,57,57,57,57,57,58
    $momento_h = intval($row_h['momento']);
    if($momento != intval($row_h['momento'])){
    //Hacer muchas cosas uju!
    }
}

How do I get this change of variable detected and be able to enter the IF?

    
asked by cignius 28.02.2018 в 16:45
source

2 answers

0

You can try this code:

$previo = NULL;
while ($row_h=mysqli_fetch_array($resultado4)){
    $cel_b = $letter_h.$cel_hrango;
    $momento_h = intval($row_h['momento']);

    if ($momento_h !== $previo) {
        //Hacer muchas cosas uju!    
    }

    $previo = $momento_h;
}

It is important to use a comparator that compares not only the value, but also the data type ( !== ).

    
answered by 28.02.2018 / 17:09
source
0

To the variable $momento_h you have to assign the value after the if since if you do it before it will always have the same value as the variable with which you want to compare it. And you must initialize it before the while so that you do not get an error.

$momento_h="";
while ($row_h=mysqli_fetch_array($resultado4)){
        $cel_b = $letter_h.$cel_hrango;
        //$row_h['momento'] es el valor de la consulta de días puede tener valores 55,55,55,55,55,56,56,56,56,56,57,57,57,57,57,58

        if($momento_h != intval($row_h['momento'])){
        //Hacer muchas cosas uju!
        }
        $momento_h = intval($row_h['momento']);
    }

Obviously, in the first iteration of the while the two variables will be different, so another condition could be set if necessary.

    
answered by 28.02.2018 в 17:01