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';
}
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 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;
}
How can I use shorthand for php's if when there are multiple elseifs?
I know how to do it with one condition, but what when there are several?
This is how it is:
if($a == 00){
echo 'Clear';
}elseif ($a == 01) {
echo 'Processing';
} elseif ($a == 10) {
echo 'Marked for delete';
}
You can of course "chain" the ternary operator, but that results in horrible code. Don't do it.
Use an if/else, a switch or possibly an associative array as appropriate. For example, you could do this:
$messages = array(
00 => 'Clear',
01 => 'Processing',
10 => 'Marked for delete',
);
echo isset($messages[$a]) ? $messages[$a] : null;
In this case this won't be at all better than the if or switch statements, but it's a useful tool to keep in mind.
The switch statement?
switch ($a) {
case 0:
echo "Clear";
break;
case 1:
echo "Processing";
break;
case 2:
echo "Marked for delete";
break;
}
alternatively you can use the ternary operator:
echo ($a == 0 ? "Clear" :
($a == 1 ? "Processing" :
($a == 2 ? "Marked for delete" : "")));
use switch
switch ($a) {
case 1:
echo "clear";
break;
case 10:
echo "marked default";
break;
default:
echo "not tracked case";
break;
}
You should NEVER do this, it is utterly unreadable but...
echo ($a==00?"Clear":($a== 01?"Processing":($a == 10?"Marked For Delete":"")));
You can do it like that:
echo ($a==0 ? 'clear' : ($a==01 ? 'Processing' : ($a==10 ? 'Marked for delete' : '' )));
But Jon is right, don't do it - as you can see, the code is ugly.
I think the switch statement might be better suited for multiple elseif's
switch ($a) {
case "00":
break;
case "Clear":
break;
default:
break;
}
Here is example of ternary operator (shorthand of if/else)
$days = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29
: ($year %400 ? 28 : 29)))
: (($month - 1) % 7 % 2 ? 30 : 31));
//returns days in the given month
But it will be confusing to work!
So i prefer to work with switch case!
switch ($type) {
case 'a':
$type = 'Type A';
break;
case 'b':
$type = 'Type B';
break;
case 'c':
$type = 'Type C';
break;
default:
break;
}
Could someone suggest the best way to have the following switch statement? I don't know that it's possible to compare two values at once, but this would be ideal:
switch($color,$size){
case "blue","small":
echo "blue and small";
break;
case "red","large";
echo "red and large";
break;
}
This could be comparable to:
if (($color == "blue") && ($size == "small")) {
echo "blue and small";
}
elseif (($color == "red") && ($size == "large")) {
echo "red and large";
}
Update
I realized that I'll need to be able to negate ($color !== "blue") and compare as opposed to equating variables to strings.
Using the new array syntax, this looks almost like what you want:
switch ([$color, $size]) {
case ['blue', 'small']:
echo 'blue and small';
break;
case ['red', 'large'];
echo 'red and large';
break;
}
You can change the order of the comparison, but this is still not ideal.
switch(true)
{
case ($color == 'blue' and $size == 'small'):
echo "blue and small";
break;
case ($color == 'red' and $size == 'large'):
echo "red and large";
break;
default:
echo 'nothing';
break;
}
Doesn't work. You could hack around it with some string concatentation:
switch($color . $size) {
case 'bluesmall': ...
case 'redlarge': ...
}
but that gets ugly pretty quick.
Found at http://www.siteduzero.com/forum/sujet/switch-a-plusieurs-variables-75351
<?php
$var1 = "variable1";
$var2 = "variable2";
$tableau = array($var1, $var2);
switch ($tableau){
case array("variable1", "variable2"):
echo "Le tableau correspond !";
break;
case array(NULL, NULL):
echo "Le tableau ne correspond pas.";
break;
}
?>
Your other option (though not pretty) is to nest the switch statements:
switch($color){
case "blue":
switch($size):
case "small":
//do something
break;
break;
}
var $var1 = "something";
var $var2 = "something_else";
switch($var1.$var2) {
case "somethingsomething_else":
...
break;
case "something...":
break;
case "......":
break;
}
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;
}
?>