Do you need break in switch when return is used?
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?
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.
return
gives the control back to the calling method, whereas break
jumps to the first instruction after the switch block.
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....
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
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. here is my example.
switch (ActiveSt)
{
case 0:
obj.Authuser(Session["UserId"].ToString());
obj.auditlog(UserID, MachineName, IpAdd, OsUser, Des, SysType, Screen, Fncname);
Session["UserId"] = null;
Response.Write("<script>alert('User authorized successfully');window.location ='frmUserPendingList.aspx';</script>");
return;
case 7:
obj.Authuser(Session["UserId"].ToString());
obj.auditlog(IssueId, MachineName, IpAdd, OsUser, Des, SysType, Screen, Fncname);
Session["UserId"] = null;
Response.Write("<script>alert('User authorized successfully');window.location ='frmUserPendingList.aspx';</script>");
return;
case 1:
Response.Write("<script>alert('User already authorized!');</script>");
errorHandle("User Already Authorized!");
return;
case 5:
Response.Write("<script>alert('User already rejected!');</script>");
errorHandle("User already rejected!");
return;
}
精彩评论