Switch, same value for multiple case - php

switch ($i) {
case A:
$letter = 'first';
break;
case B:
$letter = 'first';
break;
case C:
$letter = 'first';
break;
case D:
$letter = 'second';
break;
default:
$letter = 'third';
}
Is there any way to shorten first three cases?
They have the same values inside.

switch ($i) {
case A:
case B:
case C:
$letter = 'first';
break;
case D:
$letter = 'second';
break;
default:
$letter = 'third';
}
Yep there is. If there's no break after a case, the code below the next case is executed too.

switch ($i) {
case A:
case B:
case C:
$letter = 'first';
break;
case D:
$letter = 'second';
break;
default:
$letter = 'third';
}

Related

PHP switch strange behavoiur with zero value and strings

Going immediately to the point:
in this code I expect the default case:
<?php
$a = 0;
switch ($a) {
case "one":
echo "one";
break;
case "two":
echo "two";
break;
default:
echo "default";
break;
}
?>
I get one instead.
here expect the zero case ("two"):
<?php
$a = 0;
switch ($a) {
case "one":
echo "one";
break;
case 0:
echo "two";
break;
default:
echo "default";
break;
}
?>
I get one instead.
That appens only with zero value because here I get correctly default:
<?php
$a = 1;
switch ($a) {
case "one":
echo "one";
break;
case "two":
echo "two";
break;
default:
echo "default";
break;
}
?>
but here I get correctly zero:
<?php
$a = 0;
switch ($a) {
case 1:
echo "one";
break;
case 0:
echo "zero";
break;
default:
echo "default";
break;
}
?>
Why?

How to use Comma Separated String with switch statement

I have a string $str="1,2,4";
Also I have a switch statement
switch ($str_value) {
case '0':
$finalday='Sunday';
break;
case '1':
$finalday='Monday';
break;
case '2':
$finalday='Tuesday';
break;
case '3':
$finalday='Wednesday';
break;
case '4':
$finalday='Thursday';
break;
case '5':
$finalday='Friday';
break;
case '6':
$finalday='Saturday';
break;
default: $finalday='';
}
Using this statement from given string I want to show result
as Day is : Monday,Tuesday,Thursday
how can I do that?
Instead of switch case make an array of days
$days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Saturday'];
$str="1,2,4";
$val = [];
foreach(explode(',',$str) as $value){
$val[] = $days[$value];
}
echo implode(',',$val);//Monday,Tuesday,Thursday
Don't use a switch statement. Use preg_replace:
$final_day = $str_value;
$final_day = preg_replace('/0/', 'Sunday', $final_day);
// ...
$final_day = preg_replace('/6/', 'Saturday', $final_day);
And you have it.
If you really want to use the switch statement, explode the string into an array:
myDays = explode(",", $str);
Then loop over myDays with the switch statement in the loop.
You can use preg_replace_callback and pass a $days array to it.
That'll get the required digit(using the regex) and return day from the $days array.
Something like this -
$days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday"]
preg_replace_callback('/\d+/',function($m)use($days){return $days[$m[0]];}, $str)
#OUTPUTS -
"Monday,Tuesday,Thursday"
Hope this will help:
<?php
$str='1,2,3';
$array=explode(',',$str);
$finalday='';
foreach($array as $a){
switch ($a) {
case '0':
$finalday.=',Sunday';
break;
case '1':
$finalday.=',Monday';
break;
case '2':
$finalday.=',Tuesday';
break;
case '3':
$finalday.=',Wednesday';
break;
case '4':
$finalday.=',Thursday';
break;
case '5':
$finalday.=',Friday';
break;
case '6':
$finalday.=',Saturday';
break;
default: $finalday='';
}
}
echo substr($finalday,1);

PHP: is it possible to jump from one case to another inside a switch?

I have a switch where in very rare occasions I might need to jump to another case, I am looking for something like these:
switch($var){
case: 'a'
if($otherVar != 0){ // Any conditional, it is irrelevant
//Go to case y;
}else{
//case a
}
break;
case 'b':
//case b code
break;
case 'c':
if($otherVar2 != 0){ // Any conditional, it is irrelevant
//Go to case y;
}else{
//case c
}
break;
.
.
.
case 'x':
//case x code
break;
case 'y':
//case y code
break;
default:
// more code
break;
}
Is there any GOTO option, I red somewhere about it but can't find it, or maybe another solution? Thanks.
You need PHP 5.3 or higher, but here:
Here is the goto functionality from http://php.net/manual/en/control-structures.goto.php
<?php
$var = 'x';
$otherVar = 1;
switch($var){
case 'x':
if($otherVar != 0){ // Any conditional, it is irrelevant
goto y;
}else{
//case X
}
break;
case 'y':
y:
echo 'reached Y';
break;
default:
// more code
break;
}
?>
How about cascading (or not) based on the extra condition?
case 'x' :
if ($otherVar == 0) {
break;
}
case 'y' :
Instead of using any tricks in swtich-case, a better logic could be the following.
function func_y() {
...
}
switch($var){
case: 'x'
if($otherVar != 0){ // Any conditional, it is irrelevant
func_y();
}else{
//case X
}
break;
case 'y':
func_y();
break;
default:
// more code
break;
}

My PHP code is not working properly

I´m trying to do some program which would transfer hexa do binary. Problem is in changing of A,B,C,..,F to 10,11,12,...,15 so i can work with them as with numbers. I made this function:
function odstran_pismena($pole)
{
$dlzka = count($pole);
for ($i = 0; $i< $dlzka; $i++)
switch ($pole[$i])
{
case 0: break;
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 5: break;
case 6: break;
case 7: break;
case 8: break;
case 9: break;
case ("A" || "a"): $pole[$i] = 10;
break;
case ("B" || "b"): $pole[$i] = 11;
break;
case ("C" || "c"): $pole[$i] = 12;
break;
case ("D" || "d"): $pole[$i] = 13;
break;
case ("E" || "e"): $pole[$i] = 14;
break;
case ("F" || "f"): $pole[$i] = 15;
break;
default: $pole[$i] = "ERROR";
break;
}
return $pole;
}
First i made array from string, and now i want to change letters to numbers.
I´m testing it with this string: $test = "AbCdEf2345";
I was expecting result 10 11 12 13 14 15 2 3 4 5 but all i have is 10 10 10 10 10 10 2 3 4 5
Am I doing some mystake?(Of course I am, but where?)
("A" || "a") evaluates to boolean value 'true', so all a to f will get caught by the case ("A" || "a" ) and result in 10.
Without using the hexdec() and with minimal change to your code:
function odstran_pismena($pole)
{
$dlzka = count($pole);
for ($i = 0; $i< $dlzka; $i++)
switch ($pole[$i])
{
case 0: break;
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 5: break;
case 6: break;
case 7: break;
case 8: break;
case 9: break;
case "A":
case "a": $pole[$i] = 10;
break;
case "B":
case "b": $pole[$i] = 11;
break;
case "C":
case "c": $pole[$i] = 12;
break;
case "D":
case "d": $pole[$i] = 13;
break;
case "E":
case "e": $pole[$i] = 14;
break;
case "F":
case "f": $pole[$i] = 15;
break;
default: $pole[$i] = "ERROR";
break;
}
return $pole;
}
When you have specified case ("A" || "a"): $pole[$i] = 10; it evaluates to true. So all your chars matched against true returns true. for 'case' conditions avoid using expressions. Use the static values your comparing such as
case "A":
case "a":
$pole[$i] = 10;
break;
An easy way to help you with that would be to use a strtolower:
switch(strtolower($pole[$i])) {
case "a" :...
break;
case "b": ...
break;
}
or simply, as the first line of your function:
$pole = strtolower($pole);
This way you won't have to bother with upper/lower casing.
You should use hexdec() and replace you whole for loop with:
for ($i = 0; $i< $dlzka; $i++)
{
$pole[$i] = hexdec($pole[$i]);
}
Note that you will receive a 0 for non-valid values so you might have to check for that separately if it can happen, using for example is_numeric on the original value.

PHP Switch with 2 values

is there a way to the php's SWITCH but with 2 values? Here's what I'm looking for
switch(a, b){
case 1,2: some code... ; break;
case 3,4: some code... ; break;
case 3,6: some code... ; break;
case 5,2: some code... ; break;
case 1,3: some code... ; break;
case 8,5: some code... ; break;
}
I know this won't work, so how would i do something along these lines?
You could use an array with 2 elements since == comparison checks the array values:
$a = 3;
$b = 6;
switch([$a, $b]){
case [1, 2]: echo '1'; break;
case [3, 4]: echo '2'; break;
case [3, 6]: echo '3'; break;
case [5, 2]: echo '4'; break;
case [1, 3]: echo '5'; break;
case [8, 5]: echo '6'; break;
}
Outputs 3.
You could use some string instead if it isn't heavy processing:
$variable= "1,2";
switch($variable){
case "1,2": some code... ; break;
case "3,4": some code... ; break;
case "3,6": some code... ; break;
case "5,2": some code... ; break;
case "1,3": some code... ; break;
case "8,5": some code... ; break;
}

Categories