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?

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.