Do you need break in switch when return is used?

Php

Php Problem Overview


I was wondering if I need to use break in a switch function when return is used.

function test($string)
{
  switch($string)
  {
    case 'test1':
      return 'Test 1: ' . $string;
    case 'test2':
      return 'Test 2: ' . $string;
  }
}

I've tried it, and it works just fine without break. But is this safe?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

You do not need a break, the return stops execution of the function.

(for reference: http://php.net/manual/en/function.return.php says: > If called from within a function, the return() statement immediately ends execution of the current function

)

Solution 3 - Php

No its not necessary , because when the key word return is called it will indicate that the particular function which the switch/case was called has come to an end.

Solution 4 - Php

No, you don't need a break in a switch case statement. The break is actually optional, but use with caution.

Solution 5 - Php

You don't need it, but I would strongly advise using it in any case as good practice.

Solution 6 - Php

return gives the control back to the calling method, whereas break jumps to the first instruction after the switch block.

Solution 7 - Php

Break is just a cautionary statement used to limit the control of switch stucture from going into another case...for example if you have three case statements and value is for first case and you have used case without any break structure then all the following cases will be executed inspite of the condition being satisfied only for the first case... Return can perform the asme function so it won't be a problem if you use return in place of break because return will take control away from the switch case statement which is the need at that moment...... hope it helps....

Solution 8 - Php

Starting with PHP 8 (Nov 2020), you can use match:

<?php

function test($string) {
   return match ($string) {
      'test1' => 'Test 1: ',
      'test2' => 'Test 2: '
   } . $string;
}

Although in this case you could just use an array:

<?php

function test($string) {
   return [
      'test1' => 'Test 1: ',
      'test2' => 'Test 2: '
   ][$string] . $string;
}

https://php.net/control-structures.match

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEMCView Question on Stackoverflow
Solution 1 - PhpinsumityView Answer on Stackoverflow
Solution 2 - PhpNanneView Answer on Stackoverflow
Solution 3 - PhpGayan HewaView Answer on Stackoverflow
Solution 4 - PhpHalcyonView Answer on Stackoverflow
Solution 5 - PhpMartin BeanView Answer on Stackoverflow
Solution 6 - PhpNishant BaranwalView Answer on Stackoverflow
Solution 7 - PhpAbhimanyu SrivastavaView Answer on Stackoverflow
Solution 8 - PhpZomboView Answer on Stackoverflow