How to find previous value of PHP variable

2

I have a variable that has the rice value, shortly after receiving the rice value, it receives the value beans . How do I call the function's past value? Here is an example below:

$arroz = "arroz";

echo $arroz; = arroz

$arroz = "feijao";

echo $arroz; = feijao

echo $arroz-valor-passado = "arroz";
    
asked by anonymous 23.02.2015 / 10:48

1 answer

1

This is not possible in a variable.

The solution I suggest is to have an array. So you are accumulating values inside it and you use count($array) - 1 to fetch the last value that was inserted into the array. And the previous values following the same logic. Something like this:

$comida = array();
$comida[] = "arroz";
echo $comida[count(comida) - 1];    // arroz

$comida[] = "feijão";
echo $comida[(count($comida) - 1)]; // feijão
echo $comida[count($comida) - 2];   // arroz
    
23.02.2015 / 11:07