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

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

    Yes, try this:

    // Test variables
    $a = "content a";
    $b = "content b";
    
    // Swap $a and $b
    list($a, $b) = array($b, $a);
    

    This reminds me of python, where syntax like this is perfectly valid:

    a, b = b, a
    

    It's a shame you can't just do the above in PHP...

提交回复
热议问题