我现在正在研究其他用户的PHP代码,以便更好地理解和学习.在下面的代码中,它是用户类的一部分.当我使用if/else块进行编码时,我将它们格式化为...
if(!$this->isLoggedIn()){
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
但是在下面的代码中它更像是这样
if (! $this->isLoggedIn())
return false;
Run Code Online (Sandbox Code Playgroud)
同样在下面的函数中,您可以看到有几次可以有RETURN值.所以我的问题在于,当调用RETURN时,它之后是否运行任何代码?就像它结束那个功能的脚本那样?
在这种情况下,如果这是...
if (! $this->isLoggedIn())
return false;
Run Code Online (Sandbox Code Playgroud)
Does it continue to run the code below that?
Here is the function
<?PHP
private function logout($redir=true)
{
if (! $this->isLoggedIn())
return false;
$this->obj->session->sess_destroy();
if ($this->isCookieLoggedIn())
{
setcookie('user','', time()-36000, '/');
setcookie('pass','', time()-36000, '/');
}
if (! $redir)
return;
header('location: '.$this->homePageUrl);
die;
}
?>
Run Code Online (Sandbox Code Playgroud)
Tyl*_*ter 11
Yes.
When PHP sees a return command, it stops executing and returns it to whatever called it. This includes includes, function executions, method execution, etc.
In the following, 'Test' will never echo:
$test = "test";
return;
echo $test;
Run Code Online (Sandbox Code Playgroud)
If you are in an included file, return will stop its execution, and the file that included it will finish executing.
One of the use cases is similar to what you described:
public function echoString($string)
{
if(!is_string($string))
{
return;
}
echo $string;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1650 次 |
| 最近记录: |