Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these methods and which way properly checks for NULL?

Tags:

php

null

All three methods are a check for null,

if($sth == NULL)

if($sth === NULL)

if(is_null($sth))

which way is the proper one?

like image 706
DNB5brims Avatar asked Dec 17 '22 10:12

DNB5brims


1 Answers

They check three different things:

if ($sth == NULL)

This checks if $sth is loosely equal to null. This means that this would pass if $sth was actually 0.

if ($sth === NULL)

This checks if $sth is exactly equal to null.

if (is_null($sth))

This checks if the type of $sth is the null type (the others test the value of $sth).

The === and is_null techniques will always give the same answer; == will sometimes give a different answer.

like image 103
lonesomeday Avatar answered May 21 '23 03:05

lonesomeday