php 使用返回时需要中断开关吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6330114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do you need break in switch when return is used?
提问by EMC
I was wondering if I need to use "break" in "switch" function when "return" is used.
我想知道在使用“return”时是否需要在“switch”函数中使用“break”。
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 I need to know if this is safe. Thank you.
我试过了,它工作得很好,没有“中断”。但我需要知道这是否安全。谢谢你。
回答by insumity
Yes, you can use return
instead of break
...
是的,您可以使用return
代替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.
break
是可选的,用于防止“落入”所有其他case
语句。Soreturn
可以以类似的方式使用,作为return
函数执行的结束。
Also, if all of your case
statements are like this:
另外,如果你所有的case
陈述都是这样的:
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.
在switch
您刚刚拥有的语句之后return $result
,return find_result(...);
在 each 中使用case
将使您的代码更具可读性。
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.
最后,不要忘记添加default
案例。如果您认为您的代码永远不会达到这种default
情况,那么您可以使用该assert
函数,因为您永远无法确定。
回答by Nanne
You do not need a break, the return stops execution of the function.
您不需要中断,返回会停止函数的执行。
(for reference: http://php.net/manual/en/function.return.phpsays:
(供参考:http: //php.net/manual/en/function.return.php说:
If called from within a function, the return() statement immediately ends execution of the current function
如果从函数内部调用,则 return() 语句立即结束当前函数的执行
)
)
回答by Gayan Hewa
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.
不,它不是必需的,因为当关键字 return 被调用时,它会指示调用 switch/case 的特定函数已经结束。
回答by Halcyon
No, you don't need a break
in a switch case
statement. The break
is actually optional, but use with caution.
不,你不需要break
在switch case
声明中。该break
实际上是可选的,但谨慎使用。
回答by Abhimanyu Srivastava
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....
Break 只是一个警告语句,用于限制 switch 结构的控制进入另一个 case ......例如,如果您有三个 case 语句并且值为第一个 case 并且您使用的 case 没有任何 break 结构那么以下所有情况尽管只有第一种情况满足条件,但将执行... Return 可以执行 asme 函数,因此如果您使用 return 代替 break 不会有问题,因为 return 将控制 switch case 语句这是当时的需要......希望它有帮助......
回答by Nishant Baranwal
returngives the control back to the calling method, where as breakjumps to the first instruction after the switch block.
return将控制权交还给调用方法,其中break跳转到 switch 块之后的第一条指令。
回答by Martin Bean
You don't need it, but I would strongly advise using it in any case as good practice.
你不需要它,但我强烈建议在任何情况下使用它作为一种好的做法。