In PHP, is it possible to get a reference to the expression used in the switch statement?
For example:
switch ($_POST['id']) {
case 0:
$id = <switch expression>
break;
case 1:
$id = <switch expression>
break;
default
$id = null;
}
so if $_POST['id'] = 1
, then $id = 1
Then I can test for if (! $id) {}
Obviously you are probably thinking why not just use $id = $_POST['id']
but in the real example it looks like this
switch (strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = <switch/case expression>;
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = <switch/case expression>;
$payment_type = 'ecurrency';
break;
default:
//$payment_method = null; // taken from card number
$payment_type = 'card';
}
I dont want $payment_method
assigned.
HAD A EUREKA MOMENT WHILST WRITING THIS
Well, that works for what I was trying to achieve anyway.
switch (($payment_method = strtolower($load->post('payment_method'))))
{
case 'paypal':
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_type = 'ecurrency';
break;
default:
unset($payment_method); // taken from card number
$payment_type = 'card';
}
There are no way
use for example such way
$cases = array(0, 1, 3 ,5);
$defaultVal = 1;
$id = in_array($_POST['id'], $cases) ? $_POST['id']: $defaultVal;
AFAIK there is no such feature in PHP.
But you can do what you are looking for as easy as this:
switch (strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = 'paypal';
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = 'bitcoin';
$payment_type = 'ecurrency';
break;
default:
$payment_method = null; // taken from card type
$payment_type = 'card';
}
Actually I just realize that this probably is possible using a simple workaround:
switch ($switch_value = strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = $switch_value;
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = $switch_value;
$payment_type = 'ecurrency';
break;
default:
$payment_method = null; // taken from card type
$payment_type = 'card';
}
;-)