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

前端 未结 19 952
终归单人心
终归单人心 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 09:08

    Yes, there now exists something like that. It's not a function but a language construct (available since PHP 7.1). It allows this short syntax:

     [$a, $b] = [$b, $a];
    

    See "Square bracket syntax for array destructuring assignment" for more details.

提交回复
热议问题