如何更优雅的写这段代码
高洛峰
高洛峰 2016-11-16 13:20:41
0
5
701
<?php
$taskArray = array('1', '2', '3', '4', '5');
$deviceArray = array('Coolpad8705', 'HM2014011');
foreach($taskArray AS $t) {
    $task = $model->get($t);
    $a = false;
    foreach($deviceArray AS $device) {
        if ($model->is($device) == false) {
            $a = true;
            break;
        }
    }
    if ($a === false) continue;
}


高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(5)
三叔
foreach($taskArray AS $t) {
    $task = $model->get($t);
    // $a = false;
    // foreach($deviceArray AS $device) {
    //     if ($model->is($device) == false) {
    //         $a = true;
    //         break;
    //     }
    // }
    // if ($a === false) continue;
    if ( ! in_array($model->device(), $deviceArray))
        // do something
}


学霸
get(5);


三叔

$model->is 是什么作用?

学霸
get($t);//What's this ?
    $a = false;
    foreach($deviceArray AS $device) {
        if ($model->is($device) == false) { //false == $model->is($device)
            $a = true;
            continue; //break
        }
    }
    if ($a === false) continue; //delete this
}


三叔

两句continue;是没用的。
请区分continue;和break;的效果。
不写这两句没有任何影响,换成break;可以减少循环次数。

优雅什么的……
简洁、有效、有注释就好。
IDE会自动处理好格式的。


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!