比使用多个elseif语句更好的方法?

时间:2018-02-05 14:28:32

标签: php

是否有更简洁的方法来实现类似的结果,但没有使用所有这些elseif语句?我需要总是有一个真实的陈述,取决于动态变量。谢谢!

  $a = true;
  $b = false;
  $c = false;

  if ($a == true && $b == false && $c == false) {
    echo 'only $a is running';
  } elseif ($a == true && $b == false && $c == true) {
      echo '$a and $c are running, but not $b';  
  } elseif ($a == true && $b == true && $c == false) {
      echo '$a and $b are running, but not $c';
  } elseif ($a == false && $b == true && $c == true) {
      echo '$b and $c are running, but not $a';
  } elseif ($a == false && $b == false && $c == true) {
      echo 'only $c is running';
  } elseif ($a == false && $b == true && $c == false) {
      echo 'only $b is running';
  } else {
      echo 'nope';
  };

2 个答案:

答案 0 :(得分:2)

考虑使用一个数组,这样你就可以制作比你现在的例子更多的字母,它只会起作用:

<?php
$arr = [
  'a' => true,
  'b' => true,
  'c' => true
  ];

$running = [];
$not_running = [];
foreach($arr as $key=>$val)
  if ($val)
    $running[] = $key;
  else
    $not_running[] = $key;

if (count($running) == 0)
  echo "Nope";
else if (count($running) == 1)
  echo "Only ".$running[0]." is running";
else if (count($not_running) == 0)
  echo "All are running";
else
  echo "Only " . implode($running, ' and ') . " are running, but not ". implode($not_running, ' and ');

所以现在你可以让你的数组更大,例如:

for($i=0; $i<=25; $i++)
{
  $arr[chr($i+97)] = (bool)rand(0,1);
}

将输出如下内容:

Only c and d and e and f and g and h and l and m and n and q and u and w and y and z are running, but not a and b and i and j and k and o and p and r and s and t and v and x

答案 1 :(得分:0)

这是一种利用__toString()可以实现的潜在方式,它非常匆忙,所以你可能想要清理一下。您可以找到一个有效的示例here

您可能希望使用__toString()返回来获得所需内容,但基本上您只需要回复该集合。

附注:由于时间限制,我没有实施CountableArrayAccessIterator或任何其他有用的内置接口。如果您使用此解决方案,我建议实施它们

<?php

class Instance
{
    /** @var string */
    private $name;

    /** @var bool */
    private $active;

    public function __construct($name, $active = false)
    {
        $this->name   = $name;
        $this->active = $active;
    }

    public function turnOn()
    {
        $this->active = true;
    }

    public function turnOff()
    {
        $this->active = false;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getActive()
    {
        return $this->active;
    }

    public function __toString()
    {
        $state = $this->getActive() === true ? 'on' : 'off';
        return sprintf("%s is switched %s", $this->getName(), $state);
    }
}

class InstanceCollection
{
    private $collection = [];

    public function add(Instance $item)
    {
        $this->collection[] = $item;
    }

    public function __toString()
    {
        $on  = $this->getInstancesByState(true);
        $off = $this->getInstancesByState(false);

        return rtrim(implode(', ', $on), ', ') . rtrim(implode(', ', $off), ', ');
    }

    public function getInstancesByState($state)
    {
        return array_map(function($instance) use ($state) {
            if ($instance->getActive() === $state) {
                return $instance;
            }
        }, $this->collection);
    }
}

用法:

$instance = new Instance('eu');
$instance->turnOn();

$instance2 = new Instance('us');
$instance2->turnOff();

$collection = new InstanceCollection();
$collection->add($instance);
$collection->add($instance2);

echo $collection;

输出:

  

eu已开启,我们已关闭