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

前端 未结 19 975
终归单人心
终归单人心 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:57

    Thanks for the help. I've made this into a PHP function swap()

    function swap(&$var1, &$var2) {
        $tmp = $var1;
        $var1 = $var2;
        $var2 = $tmp;
    }
    

    Code example can be found at:

    http://liljosh.com/swap-php-variables/

提交回复
热议问题