一堆如果条件 - 简化?

时间:2018-04-16 16:57:12

标签: php arrays if-statement switch-statement

我怎样才能使我的if条件更简单?

例如:

statments:

condition 1: $row['age'] > 5;
condition 2: $row['id'] > 10;
condition 3: $row['number'] == '100';
condition 4: $row['gender'] == 'male';
condition 5: $row['state'] == 'New York';

我怎么能这样做:如果存在上述两个条件,请回显“欢迎”?我在考虑使用数组?也许别的东西会更好?

if(two of the conditions above are true) {
    echo "Welcome";
}

2 个答案:

答案 0 :(得分:2)

    if (isnull($row['age'] > 5,0) + isnull($row['id'] > 10,0) + 
        isnull($row['number'] == '100',0) + isnull($row['gender'] == 
        'male',0) + isnull($row['state'] == 'New York',0) >= 2) {
        echo "Welcome";
    }

答案 1 :(得分:0)

我的php语法生锈了,所以我的评论建议的解释应该被认为是临界伪代码:

int conditionsMet = 0;
if ($row['age'] > 5)
   conditionsMet++;
if ($row['id'] > 10)
   conditionsMet++;
if ($row['number'] == '100')
   conditionsMet++;
if ($row['gender'] == 'male')
   conditionsMet++;
if ($row['state'] == 'New York')
   conditionsMet++;

if(conditionsMet >= 2) {
    echo "Welcome";
}