PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$a = 1;
switch ($a) {
case 1 || 2 || 3;
echo "1, 2 or 3";
break;
default:
echo "Not 1, 2 or 3";
break;
}
No errors are throw back but it never runs the default condition no matter what $a is set to. Is it possible to achieve what I am trying with a switch?
Regards,
Last edited by impulse() on Wed Jul 18, 2007 6:02 am, edited 1 time in total.
$a = 1;
switch ($a) {
case 1 || 2 || 3;
echo "1, 2 or 3";
break;
default:
echo "Not 1, 2 or 3";
break;
}
No errors are throw back but it never runs the default condition no matter what $a is set to. Is it possible to achieve what I am trying with a switch?
Regards,
Use a colon : instead of a semi-colon ; at the end of each case statement.
I just learned something new and therefore my senses are heightened and endorphines are pumping, but my spidey sense tells me this isn't a very good practice.
I just learned something new and therefore my senses are heightened and endorphines are pumping, but my spidey sense tells me this isn't a very good practice.
switch($a) {
case 1:
case 2:
case 3:
echo 'yes ';
break;
default:
echo 'no ';
}
I use this technique frequently. One thing I should point out is that it may be useful to comment that the dropthrough is expected. When reviewing old code I have had occurances where people have added a break; because they thought it would be an error.
I just learned something new and therefore my senses are heightened and endorphines are pumping, but my spidey sense tells me this isn't a very good practice.
Cheers
No offense, but you really didn't know it before?
haha...none taken amigo.
Keep in mind, I never really "learned" how to program with PHP I kind of just started developing, as I had previous experience in C/C++ so the syntax is almost identical - minus this caveat.
I'm almost certain this isn't possible in C/C++ (ANSI anyways) but if it is I have *never* seen it.
Hockey wrote:I'm almost certain this isn't possible in C/C++ (ANSI anyways) but if it is I have *never* seen it.
It is. It's probably possible in any language with a switch construct since it's an expected behaviour of such constructs to make program flow logic simpler.
d11wtq wrote:It is. It's probably possible in any language with a switch construct since it's an expected behaviour of such constructs to make program flow logic simpler.
Notice that the jump-statement is required after each block, including the last block whether it is a case statement or a default statement. Unlike the C++ switch statement, C# does not support an explicit fall through from one case label to another. If you want, you can use goto a switch-case, or goto default.
I couldn't remember enough to justify whether I had actually tried but was sure I did, thus the reaosn I said it.
I'm guessing this is a caveat of M$ C++ and Borland as well. Have you actually found a reference document for ANSI C/C++ documents which say variable expressions are allowed in CASE statements? I've searched and found nothing.
Edit: I've quickly examined the EBNF of both ANSI C and C++ and from what i can tell, I'm right