Answer 533416ba631fe960060022a4 No. return jumps back directly to the function call returning the value after it and everything (in a function) that is after an executed return statement is ignored. So return itself can act as a break statement for functions and no further break is required.
No you don't need to use break . It is used to terminate a loop early, but return (within a subroutine) will also terminate any loop it is inside of. Anything that follows return is dead code.
You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.
Break will return control out of switch case.so if we don't use it then next case statements will be executed until break appears. The break statement will stop the process inside the switch.
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.
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
)
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.
No, you don't need a break
in a switch case
statement. The break
is actually optional, but use with caution.
You don't need it, but I would strongly advise using it in any case as good practice.
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....
return
gives the control back to the calling method, whereas break
jumps to the first instruction after the switch block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With