are there any faults in the following code revision (PHP)
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.
精彩评论