PHP If/ELSE或Switch/Case Statement

Har*_*M V 14 php algorithm switch-statement

我提供的输入可以是1或0

$no_required
$on_arrival
$schengen_visa
$uk_visa
$usa_visa
Run Code Online (Sandbox Code Playgroud)

我有以下情况,我想向每个用户显示唯一的消息

a b c d e
1 0 0 0 0   No Visa Required
0 1 0 0 0   Visa can be obtained on Arrival
0 0 1 0 0   You need Schengen Visa
0 0 0 1 0   You need UK visa
0 0 0 0 1   You need US visa
0 0 1 1 1   You need Either of the Visas
0 0 1 1 0   You need Schengen/UK visa
0 0 1 0 1   You need Schengen/US visa
0 0 0 1 1   You need USA/UK visa
Run Code Online (Sandbox Code Playgroud)

其中ABCDEF是上述变量.哪种是显示结果的最佳和优化方式.

dec*_*eze 17

你展示的条件可以很好地通过位掩码建模:

$messages = [
    16 => 'No Visa Required',
    8  => 'Visa can be obtained ...',
    4  => ...
];
Run Code Online (Sandbox Code Playgroud)

然后,您只需将单独的变量格式化为位掩码:

$bitmask = ($no_required ? 16 : 0)
         | ($on_arrival  ? 8  : 0)
         | ...;
Run Code Online (Sandbox Code Playgroud)

然后选择正确的信息:

echo $messages[$bitmask];
Run Code Online (Sandbox Code Playgroud)

注意:在这里使用常量而不是幻数也是必须的,所以它看起来像这样:

define('VISA_NONE',       1);
define('VISA_ON_ARRIVAL', 2);
...

$messages = [
    VISA_NONE         => 'No Visa Required',
    ...,
    VISA_US | VISA_UK => 'You need USA/UK visa'
];

// using multiplication instead of conditionals, as mentioned in the comments
$bitmask = $no_required * VISA_NONE
         | $on_arrival  * VISA_ON_ARRIVAL
         | ...;

echo $messages[$bitmask];
Run Code Online (Sandbox Code Playgroud)

将整个事物包装在适当的类中,您将拥有一个漂亮,可读,可维护,可重用的业务逻辑:

class Visa {

    const NONE       = 1;
    const ON_ARRIVAL = 2;
    ...

    protected $messages = [];

    protected $visa;

    public function __construct() {
        $this->messages = [
            static::NONE            => 'No Visa Required',
            ...,
            static::US | static::UK => 'You need USA/UK visa'
        ];
    }

    /**
     * @param int  $type    One of the class constants.
     * @param bool $enabled Whether this type of visa is required.
     */
    public function set($type, $enabled) {
        $this->visa = $this->visa | $type * (int)(bool)$enabled;
    }

    public function getMessage() {
        return $this->messages[$this->visa];
    }

}


$visa = new Visa;
$visa->set($visa::NONE,       $no_required);
$visa->set($visa::ON_ARRIVAL, $on_arrival);

echo $visa->getMessage();
Run Code Online (Sandbox Code Playgroud)


小智 11

<?php
$messages = array('10000' => 'No Visa Required', '01000' => 'Visa can be obtained on Arrival');
$no_required = '0';
$on_arrival = '1';
$schengen_visa = '0';
$uk_visa = '0';
$usa_visa = '0';

$result = "$no_required$on_arrival$schengen_visa$uk_visa$usa_visa";
if(array_key_exists($result, $messages)){
 echo $messages[$result]; //Visa can be obtained on Arrival
}

?>
Run Code Online (Sandbox Code Playgroud)


Ger*_*rry 5

我认为开关将是不错的选择:

$val=$no_required.$on_arrival.$schengen_visa.$uk_visa.$usa_visa;

switch($val)
{
    case "10000":
        echo "No Visa Required";
        break;
    case "01000"   
        echo "Visa can be obtained on Arrival.";
        break;
    case "00100":
        echo "You need Schengen Visa";
        break;
         .
         .   //Continue to add cases .
}
Run Code Online (Sandbox Code Playgroud)