PHP switch strange behavoiur with zero value and strings - php

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?

Related

Go to default case from another case in PHP

It is possible to go to default case from within some other case like this?
$a = 0;
$b = 4;
switch ($a) {
case 0:
if ($b == 5) {
echo "case 0";
break;
}
else
//go to default
case 1:
echo "case 1";
break;
default:
echo "default";
}
I tried to remove the else and was expecting that it would continue evaluating all following cases until default but it gets into case 1 then. Why is it so and how can I get to the default one?
Yes you can if you reorder the case statements:
switch ($a) {
case 1:
echo "case 1";
break;
case 0:
if ($b == 5) {
echo "case 0";
break;
}
default:
echo "default";
}
Why is it so:
it is defined, if you de not have a break statement the next case will be executed. If there is no more case, the default will be executed if one is defined
You could re-order the case statements to allow a fall through to the next option, but if there are several times you wish to do this it can become impossible or just very fragile. The alternative is to just more the common code into a function...
function defaultCase() {
echo "default";
}
switch ($a) {
case 0:
if ($b == 5) {
echo "case 0";
}
else {
defaultCase();
}
break;
case 1:
echo "case 1";
break;
default:
defaultCase();
}
I would suggest using a simple if / else as I mentioned in the comments.
However, here's another way you could do it using a switch statement, if that helps:
switch ([$a, $b]) {
case [0, 5]:
echo 'case 0';
break;
case [1, $b]:
echo 'case 1';
break;
default:
echo 'default';
}

Update PHP variable in switch statement (simple)

In PHP, I have a value outside of a switch called $chickens, equal to 1. After going through a switch statement, I want the value to change to b.
//original value of var
$chickens = 1;
switch ($chickens) {
case "0":
$chickens === "a";
break;
case "1":
$chickens === "b";
break;
case "2":
$chickens === "c";
break;
case "3":
$chickens === "b";
break;
}
//want this to be "b" defined in switch
echo $chickens;
For some reason the value does not update :(. Any Ideas
You're using the wrong operator. = is the assignment operator. === is a comparison operator:
//original value of var
$chickens = 1;
switch ($chickens) {
case "0":
$chickens = "a";
break;
case "1":
$chickens = "b";
break;
case "2":
$chickens = "c";
break;
case "3":
$chickens = "d";
break;
}
//want this to be "b" defined in switch
echo $chickens;

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

Switch, same value for multiple case

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';
}

Categories