How do I code an if else PHP statement using sessions as conditions without getting error message?
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']))
精彩评论