检查变量的最佳实践

时间:2018-12-12 07:49:23

标签: php wordpress

哪种是使用参数值的最佳实践? 以下是我在Wordpress主题内编写的php代码。

function kpn_page_banner($args) {
    if (!$args['title']) {
        $args['title'] = the_title();
    }

    !$args['title'] && $args['title'] = the_title();

    $title = $args['title'] ? $args['title'] : the_title();


    // ... 

}

1 个答案:

答案 0 :(得分:1)

不建议在未检查其存在的情况下访问array keys。如果您以这种方式进行操作,则可以获得一些Notices

if(!isset($args['title']) || ( isset($args['title']) && trim($args['title']) === '')) {
     $args['title'] = the_title();
}

此外,永远不要尝试通过排除一些非常重要的条件来减小if语句的大小,例如isset就在您的情况下。

  

查看更多http://php.net/manual/ro/function.isset.php

相关问题