这可以写得多好? PHP代码

发布于 2024-11-13 08:35:01 字数 558 浏览 2 评论 0原文

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

$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 看起来像这样:

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

如何写得更好?

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:

Please update your profile details and change password

How this can be written better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

命硬 2024-11-20 08:35:01

您可以将消息添加到数组中,然后使用 将它们连接起来

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

You can add messages to array and then join them with 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);
凉城凉梦凉人心 2024-11-20 08:35:01

怎么样:

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

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);
风蛊 2024-11-20 08:35:01

另一个建议是(如果可能的话)重构 $user->is_details_updated$user->needs_to_update_details$user->is_pass_changed code>、$user->needs_to_update_password 属性返回布尔 true/false 值。这可能会避免以后的一些调试麻烦。

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. This might save some debugging headaches later on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文