开发者

How do I code an if else PHP statement using sessions as conditions without getting error message?

开发者 https://www.devze.com 2023-03-27 18:30 出处:网络
I\'m using the following php if statement as part of my code if ($_SESSION[\'username\']) and everything is fine when the username session is set, but when it isn\'t, I get the following error mess

I'm using the following php if statement as part of my code

if ($_SESSION['username'])

and everything is fine when the username session is set, but when it isn't, I get the following error message which I would like to not have show up.

Notice: Undefined index: username in C:\xampp\htdocs\mysites\ebay_tutorial\index.php on line 12

How can I improve my cod开发者_Python百科e so that the error message doesn't show up?


This is happening because your PHP error reporting level is set to show Notice errors, which display if you try to access a key in an array which does not exist.

You can stop the error from being displayed by adding a call to isset:

if (isset($_SESSION['username']))
0

精彩评论

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