簡體   English   中英

PHP If / ELSE或Switch / Case Statement

[英]PHP If/ELSE or Switch/Case Statement

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

$no_required
$on_arrival
$schengen_visa
$uk_visa
$usa_visa

我有以下情況,我想向每個用戶顯示唯一的消息

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

其中ABCDEF是上述變量。 哪種是顯示結果的最佳和優化方式。

你展示的條件可以很好地通過位掩碼建模:

$messages = [
    16 => 'No Visa Required',
    8  => 'Visa can be obtained ...',
    4  => ...
];

然后,您只需將單獨的變量格式化為位掩碼:

$bitmask = ($no_required ? 16 : 0)
         | ($on_arrival  ? 8  : 0)
         | ...;

然后選擇正確的信息:

echo $messages[$bitmask];

注意:在這里使用常量而不是幻數也是必須的,所以它看起來像這樣:

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];

將整個事物包裝在適當的類中,您將擁有一個漂亮,可讀,可維護,可重用的業務邏輯:

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();
<?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
}

?>

我認為開關將是不錯的選擇:

$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 .
}

小提示:

為什么不將所有這些二進制數轉換為整數值,然后通過switch語句傳遞它們?

<?php

    $integer_value = convert_binary_to_integer(a,b,c,d,e);

    // I'm not sure PHP provdies a function to convert binary numbers to integers number:
    // But you can write it yourself. It's pretty easy

    switch($integer_value) {
        case 16: // As the first combination of a,b,c,d,e corresponds to number 16 
             // do the appropriate action
        break;
            // ... and so on
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM