I'm debugging some code for a client and found the following syntax:
switch ($i) {
case 0;
echo "i equals 0";
break;
case 1;
echo "i equals 1";
break;
case 2;
echo "i equals 2";
break;
}
The case statements end in semi-colons rather than colons. Turns out this does compile, but is it legit? I've never seen that syntax before.
From the documentation:
It's possible to use a semicolon instead of a colon after a case like:
switch($beer)
{
case 'tuborg';
case 'carlsberg';
case 'heineken';
echo 'Good choice';
break;
default;
echo 'Please make a new selection...';
break;
}
As you can check here, it works: http://codepad.org/hOLQP98D i think it works because it falls through
Yup, just as long as $i has a number value
Related
Can someone please explain why the case "a" is never reached in below code and why it will always execute case 0
switch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
PHP, like JavaScript or Perl, is a loosely-typed language and will attempt to guess what you want to do. In this case, it changed your string to the closest integer it could find, which is zero. In other words, "a" == 0 is a true statement in PHP.
More on this topic can be found in the PHP documentation. I suggest you typecast the value in the switch statement, or replace it with an if/elseif/else construct.
As of PHP 8.0, this behaviour has changed and now the integer value will always be changed to a string before comparison between the two types. Strictly typing and comparing your variables remains the recommended practice, however.
You can not used mix-cases in a switch statement as PHP will interpret the meaning of what you mean.
In layman's terms, it will try to find the 'value of "a"' which is not defined to the processor, and hence is 0 in this case.
Same will go for the code below:
<?php
$x = "a";
switch($x)
{
case "c":
echo "c";
break;
case 1:
echo "1";
break;
case 0:
echo "0";
break;
case "a":
echo "a";
break;
case false:
echo "false";
break;
default:
echo "def";
break;
}
?>
Documentation is available at PHP.net
The reason for this is because switch uses a loose comparison ==
That said:
if ("a" == 0) // TRUE
if ("a" == true) // TRUE
Pretty much anything else will evaluate to false. (except "a" == "a")
So, if you have the need to compare against both strings and integers, you should just convert to string for the comparison.
//$var = "a";
$var = 0;
$var = strval($var);
switch ($var) {
case '0':
echo "0";
break;
case 'a':
echo "a";
break;
}
The variable type used on case() should be same type used in switch().
<?php
switch ("a") {
case "0":
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
For integer type:
<?php
switch (1) {
case 0:
echo 0;
break;
case 1: // never reached because "a" is already matched with 0
echo 1;
break;
}
php switch statement returns invalid output :
$cond = "Night";
switch($cond){
case "Morning":
case "Night":
$name = "Good";
case "Morning" :
echo $name." Morning";
break;
case "Night" :
echo $name." Night";
break;
}
output : Good Morning
Expected Good Night
Edit (after reading replies) :
Thanks for answers.
I didn't get a clear answer.
I know i can use another statement like if else.
But logically the code should work correctly(I think).
because there's no break after 2 first cases .
but why when execution reaches to case "Morning" , the condition is true while isn't true in real?
You could do it slightly differently - as there is always going to be a good you could do this:
$cond = "Night";
$pre='Good ';
switch($cond){
case "Morning" : echo $pre." Morning"; break;
case "Night" : echo $pre." Night"; break;
}
As you didn't add break first case - execution continues and breaks after
echo $name." Morning";
But even if you add break statement for the first case - you won't reach last case:
case "Night" :
echo $name." Night";
break;
as execution already out of switch-block.
So you have to write some other code with another logic. Simple one is:
$cond = "Night";
switch($cond){
case "Morning":
case "Night":
echo "Good" . $cond;
break;
}
The switch construct is always a bit of a risk when you do not specify a break, as execution will just continue to the next cases without testing those conditions.
As stated in the docs, I emphasise in bold:
It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.
So, to be clear: PHP does not verify case conditions any more once it has started executing statements inside a case. Without a break, it will just continue with the statements of the next cases without verifying the case conditions. And so it goes from case to case, executing all statements until it finds a break or the end of the switch.
So the following code:
switch (1) {
case 1:
echo "one ";
case 2:
echo "two ";
case "hello":
echo "hello ";
}
will output:
one two hello
PHP finds the first case condition to be true and starts executing statements without evaluating any other case conditions.
This may be very counter-intuitive, and also makes the code less readable. It is best practice to put a break at the end of each case to avoid any misunderstanding.
It seems the order here is important, if you used:
$cond = "Night";
switch($cond){
case "Morning":
case "Night":
$name = "Good";
case "Night" :
echo $name." Night";
break;
case "Morning" :
echo $name." Morning";
break;
}
you would get what you expect. However as you see it can't be easily predicted what would be exact result, so depending on your needs, I would do it in different way, for example I would use 2 switches for that:
$cond = "Night";
$name = '';
switch($cond){
case "Morning":
case "Night":
$name = "Good";
break;
}
switch ($cond) {
case "Morning" :
echo $name." Morning";
break;
case "Night" :
echo $name." Night";
break;
}
or even simpler:
$cond = "Night";
$name = '';
switch($cond){
case "Morning":
case "Night":
$name = "Good";
break;
}
switch ($cond) {
case "Morning" :
case "Night" :
echo $name." ".$cond;
break;
}
Now everything will be obvious and noone will have difficulty to understand what's going on here.
To keep things organized you could do something like this. You will not need the switch
$cond = "Night";
$name = array (
'Morning' => 'Good',
'Night' => 'Good'
);
echo $name[ $cond ]." ".$cond;
In this case you don't have to add another switch case for new situations, just add an entry to the $name array
If you don't break; your switch it will continue and step into next cases.
$cond = "Night";
switch($cond){
case "Morning":
case "Night":
$name = "Good";
break; // add this
case "Morning" :
echo $name." Morning";
break;
case "Night" :
echo $name." Night";
break;
}
this happens also with other languages such as java, is called fall through
Try this code.
$cond = "Night";
$name = "Good ";
switch($cond){
case "Morning":
echo $name . $cond;
break;
case "Night":
echo $name . $cond;
break;
}
having the integer 0 as switch parameter will take the first result "foo":
$data=0; // $data is usually coming from somewhere else, set to 0 here to show the problem
switch ($data) :
case "anything":
echo "foo";
break;
case 0:
echo "zero";
break;
default:
echo "bar";
endswitch;
How do I change this, so the switch will write "zero" as expected?
The switch/case statement uses loose comparison, and, like it or not, 0 == "anything" is true:
Comparison Operators
[...] If you compare a number with a string or the comparison involves
numerical strings, then each string is converted to a number and the
comparison performed numerically. These rules also apply to the switch
statement. [...]
var_dump(0 == "a"); // 0 == 0 -> true
One solution is to change all case statements to string, and do a string comparison:
$data = 0;
switch ((string) $data): ## <- changed this
case "anything":
echo "foo";
break;
case "0": ## <- and this
echo "zero";
break;
default:
echo "bar";
endswitch;
Switch/case statement uses "loose-comparison" (i.e. ==. in this case, 0 also means false and 1 also means true. (http://www.php.net/manual/en/types.comparisons.php#types.comparisions-loose)
To avoid this problem, two solutions:
1) As suggested by #zzlalani, add quotes.
case '0': ...
2) Explicitly cast the switch statement to force a strict comparison (===)
switch((string)($data)) { ... }
Do like this
$data=0;
switch ($data)
{
case 0:
echo "bar";
break;
default:
echo "foo";
break;
}
EDIT :
How do I change this, so the switch will write "zero" as expected?
You need to move your case statement above.
$data=0;
switch ($data) :
case 0: // Moved this case to the begining
echo "zero";
break;
case "anything":
echo "foo";
break;
default:
echo "bar";
endswitch;
This is because switch does not do a "strict-type" checking.
I have a switch statement that has over 300 case statements.
case 'hello':
{ $say = 'some text'; }
break;
case 'hi':
{ $say = 'some text'; }
break;
Why is it that the break is always on a separate line? Is this required? Is there anything syntactically incorrect about me doing this:
case 'hello': { $say = 'some text'; } break;
case 'hi': { $say = 'some text'; } break;
There is nothing wrong with having the break on the same line. You also don't need the brackets.
However have you considerered rather than having a 300 case switch statment you use another method. A map of keys to values (using an array) would be faster and more maintainable.
$myArray = array(
'hello' => 'some text',
'hi' => 'some text',
);
if ( isset($myArray[$switchKey]) ){
$say = $myArray[$switchKey];
}else{
//default case
}
No, PHP is whitespace-insensitive. Semicolons and braces separate statements.
The break is usually on a separate line because it's good style not to place multiple statements on the same line, and to make the control flow of the switch statement clear.
Both your examples are possible. By the way, I don't think the brackets are necessary in this case, you could also write:
switch ($myVar) {
case 'hello': $say = 'some text'; break;
case 'hi': $say = 'some text'; break;
default: $say = 'something'; break;
}
Yes, you can put break statement in the case block , It will just seeking for break statement
to close switch case .
as an aside, I'd suggest using something like php_codesniffer for advising you re coding styles.
switch ($foo)
{
case 3 || 5:
bar();
break;
case 2:
apple();
break;
}
In the above code, is the first switch statement valid? I want it to call the function bar() if the value of $foo is either 3 or 5
You should take advantage of the fall through of switch statements:
switch ($foo)
{
case 3:
case 5:
bar();
break;
case 2:
apple();
break;
}
The PHP man page has some examples just like this.
I think what you need is:
switch ($foo)
{
case 3:
case 5:
bar();
break;
case 2:
apple();
break;
}
Interestingly, I've heard that Perl is (or maybe even has, by now) introducing this syntax, something along the lines of:
if ($a == 3 || 5)
I'm not a big fan of that syntax since I've had to write lexical parsers quite a bit and believe languages should be as unambiguous as possible. But then, Perl has solved all these sorts of problems before with those hideous tail-side ifs and ors so I suspect there'll be no trouble with it :-)
Instead, use one of the primary advantages of switch statements:
switch($foo) {
case 3:
case 5:
bar();
break;
case 2:
apple();
break;
}
Yeah, I think what you've got there is equivalent to:
<?php
$foo = 5000 ;
switch( $foo )
{
case true : // Gzipp: an '=='-style comparison is made
echo 'first one' ; // between $foo and the value in the case
break; // so for values of $foo that are "truthy"
// you get this one all the time.
case 2:
echo 'second one';
break;
default:
echo 'neither' ;
break;
}
?>
No, if you wrote case 3 || 5:, then you might as well just write case True:, which is certainly not what you wanted. You can however put case statements directly underneath each other:
switch ($foo)
{
case 3:
case 5:
bar();
break;
case 2:
apple();
break;
}