开发者

are there any faults in the following code revision (PHP)

开发者 https://www.devze.com 2023-02-14 10:30 出处:网络
since undefined equates to false, i am wondering if there are any draw backs to replacing the following code:

since undefined equates to false, i am wondering if there are any draw backs to replacing the following code:

    public static function user_access_level() {
        if (isset($_SESSION['lev_user_access'])) {
            return $_SESSION['lev_user_access'];
       开发者_如何转开发 } else {
            return false;
        }
    }

with:

    public static function user_access_level() {
        return $_SESSION['lev_user_access'];
    }


You will get a warning, if you have E_WARNING (or E_ALL, which you should be coding with).

Notice: Undefined index: lev_user_access in /your-script.php on line 7

You could use a ternary operator...

return isset($_SESSION['lev_user_access']) ? $_SESSION['lev_user_access'] : FALSE ;


false !== null but false == null

You'll also get problems if $_SESSION['lev_user_access'] is undefined (a notice will be reported depending on your error_level settings and display_errors setting.

if you want to do a one liner:

public static function user_access_level() {
    return (isset($_SESSION['lev_user_access'])) ? $_SESSION['lev_user_access']:false;
}

which may be more acceptable, but some, including myself, would argue that it's not as simple to read.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号