PHP conditionals, brackets needed?

前端 未结 7 2190
深忆病人
深忆病人 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 07:00

    In my opinion

    if ($REMOTE_ADDR == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR);
    

    is valid, but much harder to read than:

    if ($REMOTE_ADDR == "") {
        $ip = "no ip"; 
    } else {
        $ip = getHostByAddr($REMOTE_ADDR);
    }
    

提交回复
热议问题