Jump to content

[SOLVED] Checking if variable is 0 as opposed to NULL/Empty...


PeterJ

Recommended Posts

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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.