[英]How can I write better code for this condition?
I want to avoid using to nested if
statements 我想避免使用嵌套
if
语句
if (! is_null($user)){
if($user->id == $currentUser->id){
echo 'its for me';
}else{
echo 'its not for me';
}
}else{
echo 'its not for me';
}
And please note I do not want to use return
statements 并且请注意,我不想使用
return
语句
You can write something like this if the else in both cases it's the same. 如果两种情况下的其他条件都相同,则可以编写类似的内容。
if (!is_null($user) && $user->id == $currentUser->id){
echo 'its for me';
} else {
echo 'its not for me';
}
You could try something like this: 您可以尝试这样的事情:
isset function and === operator makes it better. isset函数和===运算符使它更好。
if (isset($user) && $user->id === $currentUser->id) {
echo 'its for me';
} else {
echo 'its not for me';
}
Hope this helps. 希望这可以帮助。
Peace! 和平! xD
xD
echo 'Its' . (!is_null($user) && $user->id == $currentUser->id ? ' ' : ' not') . ' for me';
在某些条件下,三元运算符可以使if / else子句非常短。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.