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

8
  • 2
    NULL=null and vice-versa unless an exact match in DB is queried. Oct 29, 2013 at 21:25
  • @FunkFortyNiner there is no DB involved on this question. Question is about the programming language. Oct 28, 2018 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. Oct 28, 2018 at 17:31
  • 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. Dec 21, 2018 at 7: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, 2019 at 14:46

4 Answers 4

137

Null is case insensitive.

From the documentation:

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

4
  • 8
    Unless an exact match in DB is queried. Oct 29, 2013 at 21:22
  • I'm having issue comparing null value getting from Database query. Can you help?
    – Razin Abid
    Dec 6, 2019 at 11:14
  • 1
    @FunkFortyNiner SQL and PHP are different languages. Their NULL have different behaviors.
    – dolmen
    Mar 30, 2021 at 9: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 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. Nov 4, 2021 at 8:24
11

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

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

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.