Php 如果需要,请进行清理和优化

Php 如果需要,请进行清理和优化,php,if-statement,Php,If Statement,我有这个代码在我的网页,但我想优化它,因为它太长了。你能告诉我写它的不同方法吗 public function __construct($css, $info, $other){ if ($info != FALSE) { echo "Info is True"; } if ($css != FALSE) { echo "Css is true"; } if ($other != FALSE) {

我有这个代码在我的网页,但我想优化它,因为它太长了。你能告诉我写它的不同方法吗

public function __construct($css, $info, $other){
    if ($info != FALSE) {
          echo "Info is True";
    }
    if ($css != FALSE) {
          echo "Css is true";
    }
    if ($other != FALSE) {
          echo "other is true";
    }
}
这只是一个例子。代码有太多的
if
条件,因为我必须检查的字段不同。有不同的方法吗

我尝试过其他方法,但没有成功。
编辑:有时变量是空的

为了避免大量的
ifs
,您可以使用单独的函数
回送所需的文本,例如:

public function __construct($css = false, $info = false, $other = false) {
    $this->echoIfTrue($css, "Css is true");
    $this->echoIfTrue($info, "Info is true");
    $this->echoIfTrue($other, "Other is true");
}

private function echoIfTrue($someVar, $textToEcho) {
    if ($someVar) {
        echo $textToEcho;
    }
}

您的代码足够清晰,但您可以尝试不同的表示方式,如:

public function __construct($css, $info, $other){
    echo $info != FALSE ? 'Info is True' : 'Info is False';
    echo $css != FALSE ? 'CSS is True' : 'CSS is False';
    echo $other != FALSE ? 'Other is True' : 'Other is False';
}

如前所述,您现有的代码足够清晰(可能您应该使用什么),但为了好玩,您可以通过使用变量使代码非常简短:-)

输出:

css is true
other is true

为什么?虽然代码在视觉上是重复的,但它简单、清晰,并且可以清晰地进行交流。因为这些变量是函数的一部分。公共函数($css,$other,$info)和一些时间是空的。你想用这些代码做什么?这没有任何意义。我认为这很简单也很清楚。@JasonMcCreary说了什么
css is true
other is true