对象中函数中的多个if语句?

时间:2016-07-18 22:36:01

标签: php class object if-statement

嗨我需要检查对象是否可以有两个参数R1或R2的0或1 - 它类似于从Obj A到Obj B的方向 - 其中1表示Obj A可以访问Obj B和0意味着Obj A无法访问Obj B.我的if语句在第一个真实语句之前有效。如何让它们检查R1和R2方向

<?php

class MyObject {

    function directionsRn($R1, $R2, $objSource, $objDest) {


        if ($R1 == 1) {

            echo("$objSource can access $objDest via R1 <br>");

            } else if ($R1 == 0) {

                       echo("SORRY, but $objSource CANNOT access $objDest via R1 <br>");

                       } else if ($R2 == 1) {

                                  echo("$objSource can access $objDest via R12<br>");

                                  }  else if ($R2 == 0) {

                                              echo("SORRY, but $objSource CANNOT access $objDest via R1 <br>");

                                              } 

    }           

}

$myObject_array[0] = new MyObject();

$myObject_array[0]->directionsRn(0, 0, 'A', 'B');

print_r($myObject_array);



?>

1 个答案:

答案 0 :(得分:0)

通过更改

来修复它
} else if ($R2 == 1) {

}

if ($R2 == 1) {

如果您正确识别代码,它将如下所示:

if ($R1 == 1) {
    echo("$objSource can access $objDest via R1 <br>");
} else if ($R1 == 0) {
    echo("SORRY, but $objSource CANNOT access $objDest via R1 <br>");
} else if ($R2 == 1) {
    echo("$objSource can access $objDest via R12<br>");
} else if ($R2 == 0) {
    echo("SORRY, but $objSource CANNOT access $objDest via R1 <br>");
}

你能看到它是if的一部分吗?

他们看起来像这样分开(注意我也改成了elseif):

if ($R1 == 1) {
    echo("$objSource can access $objDest via R1 <br>");
} elseif ($R1 == 0) {
    echo("SORRY, but $objSource CANNOT access $objDest via R1 <br>");
}

if ($R2 == 1) {
    echo("$objSource can access $objDest via R12<br>");
} elseif ($R2 == 0) {
    echo("SORRY, but $objSource CANNOT access $objDest via R1 <br>");
}