【问题标题】:How can I add a condition inside a php object?如何在 php 对象中添加条件?
【发布时间】:2017-12-09 12:03:24
【问题描述】:

我有 n 个这样的 php 对象:

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

可以在里面加条件吗?

像这样:

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

【问题讨论】:

标签: php


【解决方案1】:

你想要的是

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 以供参考

【讨论】:

  • switch 语句更好更简洁;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
相关资源
最近更新 更多