0

Say I have an SQLite table with a field named someValue. Say I use PHP to query the table and a row is returned in which someValue is NULL.

If I try to reference $row['someValue'], what happens? Does PHP return a null value, or does it throw an "undefined index"? I can't find this in the documentation. What if I use a numeric index instead of associative? If PHP returns a null, is isset() true or false?

Bonus: is this handled the same way in other databases like MySQL?

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43

1 Answers1

0

You would get a (fatal?) undefined-index error, if I remember correctly.

& you can avoid this by using:

if(!empty($row)&&!empty($row['someValue'])){
  echo $row['someValue']
}

This way we check $row first, before checking $row['someValue'] because in php conditions in if-statements are "greedy", returning once a result is found, rather than parsing all conditions.

I prefer using !empty() to using isset() because in my experience it's more reliable. I'll spare you the essay, but here's the concept, sometimes things are true when they're not & vice vera.

As per Lou's comment, if I run if( isset('') ) I recieve the following error:

Fatal error</b>: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

... but if I use if( !empty('') ) it works.

admcfajn
  • 2,013
  • 3
  • 24
  • 32