PeterJ Posted October 15, 2009 Share Posted October 15, 2009 I'm trying to write some code that will retrieve a user's access level from my database and if it doesn't return any values then I want to set the access variable to 2. I'm using... $access_id = $useraccess["0"]["access_id"]; if (!$access_id) {$access_id = '2';} Where $useraccess is the array I've got from the database. I set the variable $access_id to whatever the user's access_id field contains. Then I check the variable because if the access_id field is empty then I know the user hasn't had one specified and should be given the site default. The problem is one of the access levels is '0' which is returning an empty value and then setting it to two. Has anyone any idea how to stop this please? Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 15, 2009 Share Posted October 15, 2009 You need to do a strinct comparison using three equal signs which checks for an IDENTICAL comparison, not simply an EQUAL comparison. However, you will need to validate EXACTLY what the value is returned from your query when the user doesn't have a value set in the database. I am guessing you will want to compare against 'null'. Examples: if ($access_id === null) {$access_id = '2';} This page further explains how the comparisons are interpreted: http://php.net/manual/en/language.operators.comparison.php Quote Link to comment Share on other sites More sharing options...
PeterJ Posted October 15, 2009 Author Share Posted October 15, 2009 Thank you, that worked perfectly! 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.