[英]How better this can be written? PHP code
I am trying to generate final string to show users based on some conditions.我正在尝试生成最终字符串以根据某些条件向用户显示。
$flag=0;
$var='Please ';
if($user->is_details_updated == 'N' && $user->needs_to_update_details == "Y")
{
$var='update your profile details';
$flag=1;
}
if ($flag ==1)
{
$var=' and ';
}
if($user->is_pass_changed == 'N' && $user->needs_to_update_password == "Y")
{
$var.='change password';
}
So, If all three if
return true
then final $var
looks like this:因此,如果所有三个if
返回true
,则最终$var
如下所示:
Please update your profile details and change password请更新您的个人资料并更改密码
How this can be written better?这个怎么写更好?
You can add messages to array and then join them with and
您可以将消息添加到数组,然后将它们加入and
$var = arrray()
if($user->is_details_updated == 'N' && $user->needs_to_update_details == "Y")
{
$var[] ='update your profile details';
}
if($user->is_pass_changed == 'N' && $user->needs_to_update_password == "Y")
{
$var[]='change password';
}
echo join(" and ", $var);
How about:怎么样:
$sayings = array();
if($user->is_details_updated == 'N' && $user->needs_to_update_details == "Y") {
$sayings[] = 'update your profile details';
}
if($user->is_pass_changed == 'N' && $user->needs_to_update_password == "Y") {
$sayings[] = 'change password';
}
$var = 'Please ' . implode(' and ', $sayings);
Another suggestion would be (if possible) to refactor the $user->is_details_updated
, $user->needs_to_update_details
, $user->is_pass_changed
, $user->needs_to_update_password
properties to return boolean true
/ false
values.另一个建议是(如果可能)重构$user->is_details_updated
、 $user->needs_to_update_details
、 $user->is_pass_changed
、 $user->needs_to_update_password
属性以返回 boolean true
/ false
值。 This might save some debugging headaches later on.这可能会在以后节省一些调试麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.