101

Is there a difference between NULL and null in PHP? Sometimes they seem to be interchangeable and sometimes not.

edit: for some reason when I read the documentation linked to in the answer (before posting this question) I read it as "case sensitive" instead of "case insensitive" which was the whole reason I posted this question in the first place...

abatishchev
  • 98,240
  • 88
  • 296
  • 433
cmcculloh
  • 47,596
  • 40
  • 105
  • 130
  • 2
    `NULL=null` and vice-versa unless an exact match in DB is queried. – Funk Forty Niner Oct 29 '13 at 21:25
  • @FunkFortyNiner there is no DB involved on this question. Question is about the programming language. – Pablo Pazos Oct 28 '18 at 17:28
  • 1
    @PabloPazos why are you pinging me after 6 years have gone by? Plus, if you read my comment again, you will see probable relevance. Look at the keyword "unless" which would be a possible truth. – Funk Forty Niner Oct 28 '18 at 17:31
  • @FunkFortyNiner because comment is not accurate, no DB involved here as I said. – Pablo Pazos Oct 28 '18 at 17:32
  • @PabloPazos Where do you pick up 6 year old questions/comments is what puzzles me. In any event, my comment is worthy. – Funk Forty Niner Oct 28 '18 at 17:33
  • 2
    @PabloPazos its a bit ridiculous to even make a comment like yours, what he said is completely relevant and helpful. If someone else came along and looked at this whilst having a DB error it would help fast track a solution, those who its not relevant too will simply move on and look at another answer. – Kenziiee Flavius Dec 21 '18 at 07:47
  • 3
    @FunkFortyNiner Thanks for your comment, that was exactly what I needed to hear. I was trying to filter out a database value NULL by using isset() which usually returns false for regular nulls, but with the database value I had to use $value === NULL. I don't quite understand how this is possible, but I arrived at this page with this problem. – Jeff Mar 25 '19 at 14:46
  • @PabloPazos the word "unless" provides extra information here. Now, Funky Forty Niner makes a rather invalid comment when he complains about the difference in his post and response. Regardless of the time, had he been wrong he should've been called out on it. – I try so hard but I cry harder Apr 24 '22 at 12:21

4 Answers4

137

Null is case insensitive.

From the documentation:

There is only one value of type null, and that is the case-insensitive keyword NULL.

mbillard
  • 38,386
  • 18
  • 74
  • 98
  • 8
    Unless an exact match in DB is queried. – Funk Forty Niner Oct 29 '13 at 21:22
  • I'm having issue comparing `null` value getting from Database query. Can you help? – Razin Abid Dec 06 '19 at 11:14
  • 1
    @FunkFortyNiner SQL and PHP are different languages. Their `NULL` have different behaviors. – dolmen Mar 30 '21 at 09:40
  • 1
    For those who are now wondering whether it is better to write the keyword in upper or lower case. The PHP-FIG recommends a lowercase notation for this keyword (see https://www.php-fig.org/psr/psr-2/). I agree with this because there is less confusion with the MySQL keyword of the same name when written this way. But since these are only recommendations (some of them are controversial, e.g. indentation with spaces vs. tabs), you should know them and with this knowledge you can then decide whether to follow them or not if you haven't any guidelines yet. – Alexander Behling Nov 04 '21 at 08:24
11

There is no difference. Same type just its a case insensitive keyword. Same as True/False etc...

andreas
  • 16,357
  • 12
  • 72
  • 76
Desolator
  • 22,411
  • 20
  • 73
  • 96
3

Either will work. But the official PHP style guide, PSR-12, recommends lowercase.

https://www.php-fig.org/psr/psr-12/, Section 2.5

RedDragonWebDesign
  • 1,797
  • 2
  • 18
  • 24
0

NULL = null, which means null = NULL, both refer to each other and have only one data type null.

PHP Manual:

There is only one value of type null, and that is the case-insensitive constant null.

The consideration should be only taken when the underlying Database has specific requirements.

But if we take the example of MySQL DBMS, the null value in the column is represented as uppercase NULL, but both cases will work. For your satisfaction, you can use NULL for example.

Stack Overflow
  • 1
  • 5
  • 23
  • 51