简体   繁体   English

如何优化if语句

[英]how to optimize if else statements

 foreach ($remote_user_detail as $value){
        $user = User::where('unique_id',$value->userid)->first();

        if(count($user) > 0){
            $user->unique_id = $value->userid;
            $user->user_first_name = $value->name;
            $user->card_id = $value->cardnumber;
            $user->save();
        }else{
            $user = new User();
            $user->company_id = Auth::user()->company_id;
            $user->user_label = 2;
            $user->unique_id = $value->userid;
            $user->user_first_name = ($value->name=='') ? '' : $value->name ;
            $user->card_id = $value->cardnumber;
       $user->save();
}
}

now how should i optimize this if else because unique id user_first_name and card number are same 现在我应该如何优化这个,否则,因为唯一的标识user_first_name和卡号相同

According to your if check there is no need for an if check because if $value->name == '' you are saving empty string in $user->user_first_name = '' , so you can remove if check 根据您的if检查,不需要进行if检查,因为如果$value->name == ''您将在$user->user_first_name = ''中保存空字符串,则可以删除if检查

$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->user_first_name = $value->name;
$user->card_id = $value->cardnumber;
$user->save();

Edit for updated code base you could refactor your code as 编辑更新的代码库,您可以将代码重构为

foreach ($remote_user_detail as $value){
    $user = User::where('unique_id',$value->userid)->first();
    if(count($user) == 0){
        $user = new User();
        $user->company_id = Auth::user()->company_id;
        $user->user_label = 2;
        $user->unique_id = $value->userid;
    }
    $user->user_first_name = $value->name;
    $user->card_id = $value->cardnumber;
    $user->save();
}

As Khalid pointed out you don't need to check as you're already assigning empty value so it's a waste. 正如Khalid指出的那样,您无需检查,因为您已经在分配空值,因此很浪费。 This solution is only intended If you want to add anything else. 仅当您要添加其他任何内容时,才需要此解决方案。

Just use Ternary opertor on the $user->user_first_name like 只需在$ user-> user_first_name上使用三元运算符即可

            $user = new User();
            $user->company_id = Auth::user()->company_id;
            $user->user_label = 2;
            $user->unique_id = $value->userid;
            $user->user_first_name = ($value->name=='') ? '' : $value->name ;
            $user->card_id = $value->cardnumber;
            $user->save();

May be like this ? 可能是这样吗?

$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->card_id = $value->cardnumber;
if($value->name=='') {
    $user->user_first_name = '';
}else {
    $user->user_first_name = $value->name;
}
$user->save();

In a if statement i would put only the things which are changing according if condition. 在if语句中,我只将根据if条件改变的事物放入。 There you can apply the DRY principle. 在那里您可以应用DRY原理。

Follow this link for more details http://web-techno.net/dry-principle-explained/ 点击此链接获取更多详细信息http://web-techno.net/dry-principle-explained/

In your case I will go for the ternary operator: 在您的情况下,我将选择三元运算符:

If this is new for you, check this https://davidwalsh.name/php-shorthand-if-else-ternary-operators 如果这是您的新手,请查看此https://davidwalsh.name/php-shorthand-if-else-ternary-operators

$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->user_first_name = $value->name == '' ? '' : $value->name
$user->card_id = $value->cardnumber;
$user->save();

I recommend you create a User constructor, and just pass in the user as parameter 1, and $value as parameter 2 我建议您创建一个User构造函数,并将用户作为参数1传入,将$ value作为参数2传入。

class User {
    //definitions here
    function __construct2($user,$value) 
    { 
        company_id = $user->company_id;
        user_label = 2;
        unique_id = $value->userid;
        user_first_name = $value->name;
        card_id = $value->cardnumber;
    }
}

This highly cleans up the code for future use, if wanting to reallocate something like this. 如果要重新分配这样的代码,这将高度清理代码以备将来使用。

$user = new User(Auth::user(), $value);

Also, as shown -- no if statement is needed 同样,如图所示-不需要if语句

How I see in 2 condition $user->user_first_name is always equal to $value->name . 我在2种情况下如何看到$user->user_first_name始终等于$value->name See your self if($value->name=='') {$user->user_first_name = ''} So just write your code without if else. 查看您自己的if($value->name=='') {$user->user_first_name = ''}因此,只需编写代码即可。

$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->user_first_name = $value->name;
$user->card_id = $value->cardnumber;
$user->save();

Also, one advice for the feature if you have if else situation use PHP 7 Null coalescing operator here is the link and example 另外,如果有其他情况,请使用此功能的一个建议使用PHP 7 Null合并运算符,此处是链接和示例

http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
 foreach ($remote_user_detail as $value){
            $user = User::where('unique_id',$value->userid)->first();

            if(count($user) > 0){
                $user->unique_id = $value->userid;
                $user->user_first_name = $value->name;
                $user->card_id = $value->cardnumber;
                $user->save();
            }else{
                $user = new User();
                $user->company_id = Auth::user()->company_id;
                $user->user_label = 2;
                $user->unique_id = $value->userid;
                $user->user_first_name = ($value->name=='') ? '' : $value->name ;
                $user->card_id = $value->cardnumber;
           $user->save();
}
}

now how should i optimize this if else because unique id user_first_name and cardnumber are same 现在我应该如何优化这个,否则,因为唯一标识user_first_name和cardnumber相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM