Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP If/ELSE or Switch/Case Statement

I have give inputs which can be either 1 or 0

$no_required
$on_arrival
$schengen_visa
$uk_visa
$usa_visa

I have the Following Cases and i want to display unique message back to the user for each one of them

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

Where A B C D E F are the above variables. Which is the best and optimized way to display the results.

like image 728
Harsha M V Avatar asked Apr 02 '13 12:04

Harsha M V


People also ask

Which is better if-else or switch in PHP?

if / else if / else is more flexible (hence better), but switch is slightly faster because it just computes the condition once and then checks for the output, while if has to do this every time.

Which is better if-else or switch case?

A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.

Which is faster if or switch PHP?

Speed: A switch statement might prove to be faster than ifs provided number of cases are good. If there are only few cases, it might not effect the speed in any case. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too.

Can we use if statement in switch case in PHP?

One uses a series of if and elseif statements, and the other a switch statement. In each case, the output is the same. Here, if $i is equal to 0, PHP would execute all of the echo statements! If $i is equal to 1, PHP would execute the last two echo statements.


1 Answers

The conditions you're showing can very nicely be modeled via bit masks:

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

You then just have to format your separate variables into a bitmask:

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

Then just pick the right message:

echo $messages[$bitmask];

Note: use of constants instead of magic numbers is pretty much mandatory here as well, so it'd look like this:

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

Wrap the whole thing in an appropriate class and you have yourself a nice, readable, maintainable, reusable piece of business logic:

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();
like image 131
deceze Avatar answered Nov 16 '22 02:11

deceze