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.
Related
Basically what I'm wondering if there is a way to shorten something like this:
if ($variable == "one" || $variable == "two" || $variable == "three")
in such a way that the variable can be tested against or compared with multiple values without repeating the variable and operator every time.
For example, something along the lines of this might help:
if ($variable == "one" or "two" or "three")
or anything that results in less typing.
in_array() is what I use
if (in_array($variable, array('one','two','three'))) {
Without the need of constructing an array:
if (strstr('onetwothree', $variable))
//or case-insensitive => stristr
Of course, technically, this will return true if variable is twothr, so adding "delimiters" might be handy:
if (stristr('one/two/three', $variable))//or comma's or somehting else
$variable = 'one';
// ofc you could put the whole list in the in_array()
$list = ['one','two','three'];
if(in_array($variable,$list)){
echo "yep";
} else {
echo "nope";
}
With switch case
switch($variable){
case 'one': case 'two': case 'three':
//do something amazing here
break;
default:
//throw new Exception("You are not worth it");
break;
}
Using preg_grep could be shorter and more flexible than using in_array:
if (preg_grep("/(one|two|three)/i", array($variable))) {
// ...
}
Because the optional i pattern modifier (insensitive) can match both upper and lower case letters.
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;
}
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
So I have a variable we will say $string = 2;
if i want to check that variable for either 2 conditions I'd typically do
if($string == 2 || $string == "Hello world"):
Is there anyway to combine these two args into something shorter like
if($string(==2 || =="hello world")):
I've googled this but I cant come up with the right phrase to get back the answer I'm looking for so explaining it at this point is the easiest.
if (in_array($string, array(2, 'Hello world')))
See http://php.net/in_array.
A sick part of me wanted to answer this.
function f($a){
$args = func_get_args();
return in_array($a, array_slice($args, 1));
}
used like
if(f($str, 1, "hello world", 3, "etc")){
}
If variable have many states that can be processed in different ways, you can use switch statement:
switch($var)
{
case 1:
case 2:
//some action
break;
case 3:
break;
}
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;
}