How to Find previous PHP variable value

Asked

Viewed 400 times

2

I have a variable that has the value of rice, soon after receiving the value of rice, she gets the value ugliness. How do I call the past value of the function? Follow example below:

$arroz = "arroz";

echo $arroz; = arroz

$arroz = "feijao";

echo $arroz; = feijao

echo $arroz-valor-passado = "arroz";
  • 4

    As I know PHP you can’t do that. Only if you keep the value passed in another variable or one array of past values.

  • I didn’t understand that line echo $arroz-valor-passado = "arroz";. In addition, the use of a array might be the best choice like @Jorgeb said.

  • @Papacharlie when she does echo puts the expected result forward with the = resultado

  • @Jorgeb., I understood, could have used //, but what I didn’t understand was $arroz-valor-passado. It is a new nonexistent variable?

  • @That’s Papacharlie. That’s how she wanted it to be...

  • @Jorgeb, I didn’t understand if it was a new var or a new var $rice (subtraction) past value - got confused. Could do something using extract, depending on the use of var. It would be better to know the application.

Show 1 more comment

1 answer

1

This is not possible in a variable.

The solution I suggest is to have an array. So you accumulate values within it and use the 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

Browser other questions tagged

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