这可以写得多好吗?PHP代码

Dar*_*ric 2 php if-statement conditional-statements

我正在尝试根据某些条件生成最终字符串以显示用户.

$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';
}
Run Code Online (Sandbox Code Playgroud)

所以,如果所有三个if返回,true那么final $var看起来像这样:

请更新您的个人资料详情并更改密码

如何写得更好?

sko*_*ine 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);
Run Code Online (Sandbox Code Playgroud)