2016-02-06 67 views
-1

我只是把這個縮短了,但如果在這個if語句中有很多條件,有沒有辦法縮短它呢?有沒有辦法縮短這個如果陳述條件? PHP

if(($post->id <= 37) || ($post->id == 42) || ($post->id == 44) || ($post->id == 45) || ($post->id == 46))){ 
    //code here 
} 

一切都會ID,但數量會有所不同,有沒有範圍或舊/偶數只是隨機量

有沒有辦法把它縮短?

+2

if if id> 18' then id' always always than 4 and 12? – samrap

+2

如果'id'大於18,則不需要檢查它是否大於4和12 ..也許您可以提供您正在嘗試執行的功能示例? – chris85

+0

@samrap對不起,只是讓它隨機更早我編輯的代碼,但這將越來越多,因爲有時候id不會是sequal – Dora

回答

2
if($post->id <= 37 || in_array($post->id, array(42,44,45,46)))