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.
Related
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';
}
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.
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;
}
This is the switch statement.
Only if the variable $thumbs_number is less than -1 (e.g. -2, -3, -4, etc.), the class bad should output.
But right now, the class bad is also being output when $thumbs_number is 0 (-1 and 1 have the right class: average).
<div class="topic-like-count
<?php // Apply style based on number of votes
switch ($thumbs_number) {
case ($thumbs_number < -1): echo ' bad'; break;
case ($thumbs_number == 0):
case ($thumbs_number == 1): echo ' average'; break;
case ($thumbs_number == 2):
case ($thumbs_number == 3): echo ' good'; break;
case ($thumbs_number == 4):
case ($thumbs_number == 5): echo ' great'; break;
case ($thumbs_number == 6):
case ($thumbs_number == 7): echo ' excellent'; break;
case ($thumbs_number > 7): echo ' brilliant'; break;
}
?>
">
What is happening?
You a misusing the switch statement.
Each case statement compares the result of the expression to the value passed to switch. So here you are comparing $thumbs_number to the result of ($thumbs_number < -1), which is either true or false.
Do this instead:
switch ($thumbs_number) {
case 0:
case 1:
echo "average";
break;
case 2:
case 3:
echo "good";
break;
....
default:
if ($thumbs_number <= -1) echo "bad";
else if ($thumbs_number > 7) echo "brillant";
}
I faced a similar problem and I was reluctant to write a long if-else if block. Searching for an answer led me to this page.
With this technique, all you need to do is replacing
switch ($thumbs_number)
with
switch (true)
Here is my version of the code:
Is there a way to include multiple cases inside a switch method in php?
The switch statement works by evaluating each case expression in turn and comparing the result to the switch expression. If the two expressions are equivalent, the case block is executed (within the constraints established by break/continue constructs). You can use this fact to include arbitrary boolean expressions as case expressions. For example:
<?php
$i = 3;
$k = 'hello world';
switch (true) {
case 3 == $i and $k == 'hi there';
echo "first case is true\n";
break;
case 3 == $i and $k == 'hello world';
echo "second case is true\n";
break;
} //switch
?>
This outputs:
second case is true
I don't use this sort of construction very often (instead preferring to avoid such complex logic), but it sometimes comes up where a complicated if-then statement might otherwise be used, and can make such snippets much easier to read.
What's wrong with simply nesting switches?
$i = 1;
$j = 10;
switch($i) {
case 2:
echo "The value is 2";
break;
case 1:
switch($j) {
case 10:
echo "Exception Case";
break;
default:
echo "The value is 1";
break;
}
break;
default:
echo "Invalid";
break;
}
Yes it is possible
Here is an example to start with
<?
$i = 1;
$j = 10;
switch($i) {
case "2":
echo "The value is 2";
break;
case ($i==1 && $j==10):
echo "Your exceptional Switch case is triggered";
break;
default:
echo "Invalid";
break;
}
?>