我正在尝试根据某些条件生成最终字符串以显示用户。
$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';
}
所以,如果所有三个if
都返回true
,那么最终$var
看起来像这样:
请更新您的个人资料详情并更改密码
如何更好地写出来?
答案 0 :(得分:6)
您可以向群组添加消息,然后将其与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);
答案 1 :(得分:3)
怎么样:
$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);
答案 2 :(得分:0)
另一个建议是(如果可能)重构$user->is_details_updated
,$user->needs_to_update_details
,$user->is_pass_changed
,$user->needs_to_update_password
属性以返回布尔值true
/ {{1值。这可能会在以后节省一些调试麻烦。