如何在php对象中添加条件?

时间:2017-12-09 12:03:25

标签: php

我有这样的php对象:

        $test
        ->key1(1)
        ->key2(1)
        ->key3(1);

是否可以在其中添加条件?

像这样:

   $test
   if(true) ->key1(1)
   ->key2(1)
   if(true) ->key3(1);

1 个答案:

答案 0 :(得分:0)

你想要的是

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if this condition is true;
} else {
    code to be executed if all conditions are false;
}

例如

if ($YOUR_KEY == "key1") {
    //if key1 then do something
} elseif ($YOUR_KEY == "key2") {
    //if key2 then do something
} else {
    //if none condition work than do something else
}

此处$YOUR_KEY是您的变量,"key1"是您的值

检查www.w3schools.comhttp://php.net/manual以获取参考