This question already has answers here:
PHP- Switch case statement with conditional switch
(3 answers)
Closed 8 years ago.
Here always display "That was fast!" Why is this? Why output should not be "That's slooooow"?
$totaltime = 12;
switch ($totaltime<=13) {
case 1:
echo "That was fast!";
break;
case 5:
echo "Not fast!";
break;
case 12:
echo "That's slooooow";
break;
case 15:
echo "That's too slooooow";
break;
}
You are switching on this:
$totaltime<=13
which is true, so it comes to 1, and 1==true is true, so it 'triggers'.
Don't you mean just this?
switch ($totaltime) {
case 1:
echo "That was fast!";
break;
case 5:
echo "Not fast!";
break;
case 12:
echo "That's slooooow";
break;
case 15:
echo "That's too slooooow";
break;
}
($totaltime<=13) is evaluated to 1 so that is why you end up in the first case, change code to:
$totaltime = 12;
switch ($totaltime) {
case 1:
echo "That was fast!";
break;
case 5:
echo "Not fast!";
break;
case 12:
echo "That's slooooow";
break;
case 15:
echo "That's too slooooow";
break;
}
YOu are using a conditional for a switch statement. It should be this:
$totaltime = 12;
switch ($totaltime) {
case 1:
echo "That was fast!";
break;
case 5:
echo "Not fast!";
break;
case 12:
echo "That's slooooow";
break;
case 15:
echo "That's too slooooow";
break;
}
This will work:
$totaltime = 12;
switch ($totaltime) {
case 1:
echo "That was fast!";
break;
case 5:
echo "Not fast!";
break;
case 12:
echo "That's slooooow";
break;
case 15:
echo "That's too slooooow";
break;
}
I'm not sure but i think ($totaltime<=13) is equal true or 1 .you should use only $totaltime.
Related
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?
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);
Can i put conditional statement within switch statement. ex - switch ($totaltime<=13) Other than php how about other languages compatibility with it?
$totaltime=15;
switch ($totaltime<=13) {
case ($totaltime <= 1):
echo "That was fast!";
break;
case ($totaltime <= 5):
echo "Not fast!";
break;
case ($totaltime >= 10 && $totaltime<=15):
echo "That's slooooow";
break;
}
Edit
$totaltime=12;
switch (false) {
case ($totaltime <= 1):
echo "That was fast!";
break;
case ($totaltime <= 5):
echo "Not fast!";
break;
case ($totaltime >= 10 && $totaltime<=13):
echo "That's slooooow";
break;
default: // do nothing break;
}
Gentleman in this case why alwyas show output as "That was fast!"?
Switch only checks if the first condition is equal to the second, this way:
switch (CONDITION) {
case CONDITION2:
echo "CONDITION is equal to CONDITION2";
break;
}
So you have to do it this way:
switch (true) {
case $totaltime <= 1: #This checks if true (first condition) is equal to $totaltime <= 1 (second condition), so if $totaltime is <= 1 (true), is the same as checking true == true.
echo "That was fast!";
break;
case $totaltime <= 5:
echo "Not fast!";
break;
case $totaltime >= 10 && $totaltime<=13:
echo "That's slooooow";
break;
}
Instead of this i'll go for if-elseif statements. Is easier to understand at first sight:
if ($totaltime <= 1) {
echo "That was fast!";
} elseif($totaltime <= 5) {
echo "Not fast!";
} elseif($totaltime >= 10 && $totaltime<=13) {
echo "That's slooooow";
}
Yes you can (except for the comparison within switch)
$totaltime=12;
switch (true) {
case ($totaltime <= 1):
echo "That was fast!";
break;
case ($totaltime <= 5):
echo "Not fast!";
break;
case ($totaltime >= 10 && $totaltime<=13):
echo "That's slooooow";
break;
default:
// do nothing
break;
}
Yes you can, from the PHP's switch documentation:
The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values
When the case has constant value it's just like saying, case value == switch value, but you can have more complex expressions for a case.
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;
}
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.