PHP assignment with a default value

前端 未结 5 2134
春和景丽
春和景丽 2021-02-20 01:48

What\'s a nicer way to do the following, that doesn\'t call f() twice?

$x = f() ? f() : \'default\';
5条回答
  •  情话喂你
    2021-02-20 02:14

    You could save it to a variable. Testcase:

    function test() {
            echo 'here';
            return 1;
    }
    
    $t = test();
    $x = $t ? $t : 0;
    echo $x;
    

提交回复
热议问题