PHP conditionals, brackets needed?

前端 未结 7 2195
深忆病人
深忆病人 2020-11-28 05:55

I was just browsing a forum and someone asked about a PHP file they had found on the web. It has several spots like this in the code:

if ($REMOTE_ADDR == \"\"

7条回答
  •  攒了一身酷
    2020-11-28 06:53

    9 years on and I'm surprised no-one's mentioned the ternary operator:

    $ip = ($REMOTE_ADDR == "") ? "no ip" : getHostByAddr($REMOTE_ADDR);
    

    Much clearer for assignment IMHO - because it leads out with the variable being assigned to, as for usual variable assignment.

提交回复
热议问题