2013-05-21 50 views
0

什麼是更好的方式來格式化下面的代碼行。如果沒有格式化,其他方法也會做。在php中格式化long if語句

if (
    array_key_exists('type',$handle) && 
    array_key_exists('parent',$handle) && 
    array_key_exists('userName',$handle) && 
    array_key_exists('userId',$handle) && 
    array_key_exists('countryCode',$handle) 
) 


if(
    ctype_digit($listType) && 
    ctype_digit($listParent) && 
    (ctype_alnum($listUserName) && (strlen($listUserName) >=5 && strlen($listUserName) <=24)) && ctype_digit($listUserId) && 
     (ctype_alpha($listCountryCode) && 
    array_key_exists($listCountryCode, $countries)) 
) 

此外,是這樣的事情在PHP中可行嗎?

bool fTest1 = A == B ; 
bool fTest2 = C ; 
bool fTest3 = f(1,2,3) ; 
bool fSuccess = (fTest1 | ftest2) & fTest3 ; 
if (fSuccess) 
... 
+1

是的,這是非常可行的 –

回答

3

您可以使用array_intersect,然後做一個count避免一切if條款。

$arr1 = array("a" => "type", "parent", "username", "userId", "countryCode") 
$intersect = array_intersect($handle, $arr1) 
if(count($intersect) == count($arr1)){ 
    //Your logic goes here. 
} 

此外,你所有的上面的「可行」的例子看起來對我有效。

2

第一個已經在循環中來完成,可能與一些array_intersect家庭功能
第二個糖已拆分成獨立的報表都各有其獨特的錯誤消息
最後一個是相當可行的:

$fTest1 = $A == $B; 

等等