2

I have data in my mysql fields which are NULL

But in php when getting the data into an array how come I can compare the value of it without first using php's isset?

Example,

$var = null;

if ($var == 123123) // GENERATES ERROR AS DIDN'T CHECK IF IT WAS SET FIRST

But in mysql

$q = $dbc -> prepare("SELECT nullColumn FROM table_1");
$q -> execute();
$null = $q -> fetch(PDO::FETCH_ASSOC);
if ($null['nullColumn'] == 2312312) // DOESN'T GENERATE ERROR EVEN THOUGH VALUE IS NULL

I have also used var_dump on the value and it returns NULL

2
  • 1
    why don't you want to use isset? please provide a code example so that we can understand your use case Commented Mar 4, 2012 at 23:48
  • 1
    You don't have to use isset if your value is null. You might be mixing something up here. Care for an illustrative code sample? Commented Mar 4, 2012 at 23:50

2 Answers 2

3
$var = null;

if ($var == 123123) // GENERATES ERROR AS DIDN'T CHECK IF IT WAS SET FIRST

This premise is false! The above code will not generate an error, because the variable $var exists.

Trying to use a variable which does not exist, i.e. which hasn't been declared anywhere, generates an error. Trying to use a declared variable whose value is null is perfectly legit.

See The Definitive Guide To PHP's isset And empty for an in-depth look into this topic.

Sign up to request clarification or add additional context in comments.

Comments

0

I guess depends on the driver. PDO fetches NULL for NULL fields. Don't have to use isset, use is_null($field) or null === $field

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.