简体   繁体   English

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

[英]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.

相关问题 如何在adonisJS中翻译从php编写的代码? - How can i translate this code written from php in adonisJS? 为了将php外部数据添加到jquery中,如何更好地编写此代码? - How would this be better written For adding php external data to a jquery select 我们如何使用在网站上编写和上传的PHP代码运行C代码? - How can we run a C code using php code written and uploaded on a website? 如何保存用Wordpress页面编写的PHP代码 - How to save PHP code written in Wordpress page 如何对如下编写的 PHP 代码进行反混淆处理? - How to deobfuscate a PHP code that is written as below? 我该如何改善这段代码? - How can I better this code? 我们如何在编写为 ajax function 的 php 代码中显示 JS 警报 - how can we display JS alert in a php code written as ajax function 如何在 Java/ASP.NEt/Python 中编写代码以使用用 PHP 编写的基于组件的库? - How can I write code in Java/ASP.NEt/Python for using a component based library written in PHP? 这个检查URI方案的PHP正则表达式能否更好地编写http,https和shttp协议? - Can this PHP regex that checks URI schemes check http, https and shttp protocols be written better? 如何格式化PHP代码以更好地显示它 - How to format the php code to display this better
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM