Do you need break in switch when return is used?

Yes, you can use return instead of break

break is optional and is used to prevent “falling” through all the other case statements. So return can be used in a similar fashion, as return ends the function execution.

Also, if all of your case statements are like this:

case 'foo':
   $result = find_result(...);
   break;

And after the switch statement you just have return $result, using return find_result(...); in each case will make your code much more readable.

Lastly, don’t forget to add the default case. If you think your code will never reach the default case then you could use the assert function, because you can never be sure.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)