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.
Due to the fact that "switch" does no comparison, it is slightly faster.
The PHP switch StatementUse the switch statement to select one of many blocks of code to be executed.
PHP doesn't support this syntax. Only scalar values allowed for cases.
The switch statement compares an expression with the value in each case. If the expression equals a value in a case, e.g., value1 , PHP executes the code block in the matching case until it encounters the first break statement.
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' :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With