Update PHP variable in switch statement (simple) - php

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;

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

PHP: issue with switch statement (wrong return)

I have this method:
private function convertStatusStringToIntZeroOrOne(string $status)
{
$status = strtolower($status);
switch ($status) {
case "off":
case "0":
case 0:
$int_status = 0;
break;
case "on":
case "1":
case 1:
$int_status = 1;
break;
default:
$int_status = 1;
break;
}
return $int_status;
}
The $status parameter, when is the string "On" (with O letter capitalize), return 0 (zero).
Of course, I need return as 1.
Thank you
As you had numeric 0 and 1 in the options of the switch it was using a numeric comparison - "on" to a number is 0 and so it matched against 0.
As you have the parameter as type string a number would be converted to a string, so remove the numeric comparisons...
function convertStatusStringToIntZeroOrOne(string $status)
{
$status = strtolower($status);
switch ($status) {
case "off":
case "0":
$int_status = 0;
break;
case "on":
case "1":
$int_status = 1;
break;
default:
$int_status = 1;
break;
}
return $int_status;
}
echo convertStatusStringToIntZeroOrOne("On");
Although you could reduce the function to...
function convertStatusStringToIntZeroOrOne(string $status)
{
$status = strtolower($status);
return ($status == "off" || $status == 0)?0:1;
}

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?

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.

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