Having conditions in Switch Case: Is it same as Elseif?

Hi,

I like to know whether following implementation is same as of a corresponding Elseif implementation.


$score = 55;

switch ($score) {

	case ($score>70):
		echo 'Above 70';
		break;
		
	case ($score>50):
		echo 'Above 50';
		break;

}

The reason is, in each case statement, there is a condition. If it was like below it could have been more efficient than an Elseif. But with these conditions, I am wondering whether it’s same as Elseif in efficiency wise.

Any feedback is greatly appreciated.


$score = 50;

switch ($score) {

	case 70:
		echo 'Score is 70';
		break;
		
	case 50:
		echo 'Score is 50';
		break;

}

It’s similar to elseif, but not the same.

The difference is that sometimes you would not want to use the break; (on purpose) in which case the flow of execution continues to the next ‘case’

Also if you just forget to put ‘break;’ where you actually needed it, then you will have some strange results but there will not be any errors in php because ‘break;’ is not required. Just remember that.

define “efficiency” here

is your problem real or imagined one?

By the way, your first example is not written properly.
if you want to test for things like :
$score > 50 or $score > 70, then you don’t use $score in ‘switch’, you use true, like this:

switch (true)
{

case ($score > 50):
// good
break;

case ($score > 70):
// excellent
break;
}

There are various types of branch statement like ‘if’, ‘elseif’ ‘?’ and ‘switch’. You should use them where they fit best.

Thanks for the responses.

@Shrapnel_N5

I meant the number of conditions to be executed. If we use Switch Case just to compare with set of values as in my second example, there is no condition to be executed. So, it seemed to be more efficient to me.

Which one to use?
I think its effective to use switch statements if you have multiple choices(3 or more cases).
It also makes your code easier to read.

Performance?
In today’s modern world, the performance difference is actually negligible what it matters is the development time.
Performance can be increased by hardware tuning but for the software development you have to code in a RAD way.

NextStar, trust me.
Any number of conditions which can be written by hand, means nothing. Really nothing.
You can start trouble yourself if you have hundreds of thousands of conditions. If there are less, there are no single reason to worry about efficiency.

There are thousands of real efficiency problems to worry about. Mostly of data processing.

@PHPycho and Shrapnel_N5,

I understand your point on having readable code over attempts of micro-seconds of performance improvements and I totally agree.

But whenever it allows me to have clean code with optimization, I would go for that even if it doesn’t bring any significant performance improvement. Reason is that keeps me alert on efficiency and I would handle big performance issues with more care.

For an example, I always write ‘for loop’ in following way


$count = count($array);

for ($i=0; $i<$count; $i++) {

}

Instead of following way,


for ($i=0; $i<count($array); $i++) {

}

Since it saves the execution time of running count() function in each iteration. But if it was a mess of code to figure out and assign the count to $count, I won’t go for that.

What do you think about this philosophy?

Remember, there’s always other ways to write the same thing as well. I may do something like this if I’m going to have a lot of if elses, or switch, that are written the same way.


$score = 51;
$scores = array(50, 70, 80, 90); // Lowest to highest

foreach($scores as $num) {
  if ($score > $num) {
    $result = 'Score is above ' . $num;
  }
}

echo $result;

If we pretend you wanted your number to be rounded down to the nearest ten, the code is even simpler:


$score = 55;
$tenth = floor($score/10) * 10; // 50

echo 'Score is above ' . $tenth;

@tmapm

The only problem with your first example is that if the score is 124, it will output the following:


// assuming the insertion of an undocumented newline chr :)
Score is above 50
Score is above 70
Score is above 80
Score is above 90

To my thinking, this is neither the intended nor desired output. If I were to script a score comparison of this nature, I would be using a switch statement; but more along these lines:


$score = 55;
$displayScore = "The score was above ";
switch (true) {
  case ($score > 50):
    $scoreLevel = 50;
    break;
  case ($score > 100):
    $scoreLevel = 100;
    break;
  case ($score > 1000):
    $scoreLevel = 1000;
    break;
  case ($score > 1000000):
    $scoreLevel = "1,000,000!";
    $displayScore = "Did you cheat? Your score exceeded ";
    break;
  default:
    $displayScore = "The score was below the threshhold.";
    $scoreLevel = "";
}
echo $displayScore . $scoreLevel;

The output of my first example for the input 124 will actually be:


Score is above 90.

I override the $result var and don’t echo until outside the loop…


I didn’t know what numbers of scores he wanted, so it would work perfectly fine if the score limits were added in there. I simply stopped at 90, figuring the example would be enough.

Changing the array to be


$scores = array(50, 100, 1000, 1000000);

in my first example would pretty much do your example with less code… (minus the display message, which would need some adjustment…)

Anyway, the proof of concept is there. I’m sure NextStar has what he needs to do what he’s gotta do.

You’re right. Silly Dave. :confused:

No clue why I read an assignment as an echo.