Difference between `if (isset($_SESSION))` and `if ($_SESSION)`?

前端 未结 3 2011
小蘑菇
小蘑菇 2021-01-18 02:11

I\'ve noticed that frequently people simply write


while I have been using:



        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 02:36

    In PHP, if a variable does not exist (is "unset") then PHP will spit out an E_NOTICE error, create the missing variable, and assign it to NULL.

    If you do not want your scripts filled with annoying error messages that leak information about your server and script - then use isset() to test before trying to access a value.

    Basically, you're asking PHP to get username out of $_SESSION and PHP is like, "there is no username"!

提交回复
热议问题