这本书写得有多好?PHP代码

这本书写得有多好?PHP代码,php,if-statement,conditional-statements,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_p

我试图根据一些条件生成最终字符串来显示用户

$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
如下所示:

请更新您的个人资料详细信息并更改密码


如何编写得更好?

您可以将消息添加到数组中,然后使用

$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);
那么:

$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);

另一个建议是(如果可能的话)重构
$user->is\u details\u updated
$user->needs\u to\u update\u details
$user->is\u pass\u changed
$user->needs\u to\u update\u password
属性以返回布尔值
真值。这可能会在以后的调试中省去一些麻烦。

该死,刚刚被打败了+1:).@skowron行而不是“U”,请开始在堆栈溢出上使用“you”。“聊天说话”在这里是特别禁止的。@meagar很抱歉,我以前经常这样写。但是如果你想改变它,你可以在另一个StackExchange页面上发布,这个问题比这里更适合那里。