PHP: Exchange Value of Variables
Written by Administrator   
giovedì, 31 dicembre 2009

When exchanging the values of two variables, a third 'temp' variable is generally constructed to temporarily store one of the variables while the exchange is performed.
Here below a simple example:

if ($exchange) {
  $temp = $v1;
  $v1 = $v2;
  $v2 = $temp
}

There is however a more elegant way in PHP using the language construct list:

if ($exchange) {
  list($v1,$v2) = array($v2,$v1);
}

Last Updated ( giovedì, 31 dicembre 2009 )