davidz Posted September 12, 2007 Share Posted September 12, 2007 My question if more of a "good practice" type of thing. Let say I have a session variable $_SESSION['username']. And I want to display some text based upon the fact that this variable exists. So if I do this: <?php if ($_SESSION['username'] == 'bob@domain.com') { echo "Hello World"; } ?> When I do this, I get PHP Notice: Undefined index: username in my log messages. Now granted this is just a notice and not an error. So, my question is, is there anything wrong with this? Or is it really necessary to do checks like isset()?? Thanks, David Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2007 Share Posted September 12, 2007 use isset(). Not checking is poor programming practice. Quote Link to comment Share on other sites More sharing options...
davidz Posted September 12, 2007 Author Share Posted September 12, 2007 Ok, so is there a difference (following good programming practice) in either one of these? <?php if (isset($_SESSION['username']) == True){ echo "Yes it was set" } ?> <?php if (isset($_SESSION['username'])){ echo "Yes it was set" } ?> Or these: <?php if (isset($_SESSION['username']) == False){ echo "No it was not set" } ?> <?php if (!isset($_SESSION['username'])){ echo "No it was not set" } ?> Thanks, David Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2007 Share Posted September 12, 2007 It depends on what you're trying to do.The first one in each set is a bit more clear, but it doesn't really matter. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.