Is there a PHP function for swapping the values of two variables?

前端 未结 19 958
终归单人心
终归单人心 2020-12-02 08:36

Say for instance I have ...

$var1 = \"ABC\"
$var2 = 123

and under certain conditions I want to sw

19条回答
  •  被撕碎了的回忆
    2020-12-02 08:49

    If both variables are integers you can use mathematical approach:

    $a = 7; $b = 10; $a = $a + $b; $b = $a - $b; $a = $a - $b;
    

    Good blog post - http://booleandreams.wordpress.com/2008/07/30/how-to-swap-values-of-two-variables-without-using-a-third-variable/

提交回复
热议问题