Null OR an empty string?

if (!empty($user)) {}

Use empty().


After realizing that $user ~= $_POST['user'] (thanks matt):

var uservariable='<?php 
    echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
Answer from John Green on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.is-null.php
PHP: is_null - Manual
A second look into the PHP specs tells that is_null() checks whether a value is null or not. So, you may pass any VALUE to it, eg. the result of a function. isset() on the other hand is supposed to check for a VARIABLE's existence, which makes ...
Discussions

empty, is_null, isset comparisons
That last bit of code is horrific and you should never write code that leads to a comparison like that. If you ever run into a situation where you need a statement like that you best review the rest of your code. More on reddit.com
๐ŸŒ r/PHP
44
8
August 24, 2023
Difference between NULL and null in PHP - Stack Overflow
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 More on stackoverflow.com
๐ŸŒ stackoverflow.com
Null vs. False vs. 0 in PHP - Stack Overflow
I am told that good developers can spot/utilize the difference between Null and False and 0 and all the other good "nothing" entities. What is the difference, specifically in PHP? Does it have something to do with ===? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Better way to check if values are empty?
It's rather interesting why this happens to you, given I never had to write a line like this in my 10+ years of work with PHP (also, god damn it, 10 years). In fact, I can't remember using empty() recently. I use isset() mostly. My point is, maybe the solution can be refactoring whatever is causing you to write those lines, so if you want to share more, let us know. More on reddit.com
๐ŸŒ r/PHP
62
20
August 12, 2016
People also ask

What is the difference between null, undefined, and empty in PHP?
  • null: A variable is null when it has been explicitly assigned null or has not been assigned any value.โ€‹
  • undefined: A variable is considered undefined if it has never been declared or assigned a value. Accessing such a variable will result in a notice-level error.โ€‹
  • empty: A variable is considered empty if it holds a falsy value, such as "" (empty string), 0 (integer zero), 0.0 (float zero), "0" (string zero), false, array() (empty array), or null. The empty() function can be used to check for these values.โ€‹
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php null: how to assign and check for null values
PHP Null: How to Assign and Check for Null Values - FlatCoding
How do I set a variable to null in PHP?
You can set a variable to null by explicitly assigning null to it:โ€‹
$variable = null;
Alternatively, you can use the unset() function to destroy the variable, which will also set it to null:โ€‹
unset($variable);
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php null: how to assign and check for null values
PHP Null: How to Assign and Check for Null Values - FlatCoding
How can I check if a variable is null in PHP?
PHP provides multiple ways to check if a variable is null:โ€‹ Using is_null() function:
  $variable = null;
  if (is_null($variable)) {
      echo "Variable is null";
  }
Using strict comparison (===):
  $variable = null;
  if ($variable === null) {
      echo "Variable is null";
  }
Using isset() function: $variable = null; if (!isset($variable)) { echo "Variable is null or not set"; }
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php null: how to assign and check for null values
PHP Null: How to Assign and Check for Null Values - FlatCoding
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_var_is_null.asp
PHP is_null() Function
This function returns true (1) if the variable is NULL, otherwise it returns false/nothing. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to ...
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ php null: how to assign and check for null values
PHP Null: How to Assign and Check for Null Values - FlatCoding
June 29, 2025 - Letโ€™s start with how its values work in PHP. PHP null represents a variable with no value. A variable is null if: It is not assigned any value.
๐ŸŒ
Medium
medium.com โ€บ @jochelle.mendonca โ€บ phps-isset-empty-and-is-null-our-friendly-assets-7c2a40863da6
PHPโ€™s isset, empty, and is_null: Our friendly assets
March 15, 2024 - Itโ€™s like asking, โ€œHey, is this variable set and not null?โ€ ยท It returns true if the variable exists and has a value other than null. 2. empty: Now, empty is like your reliable Swiss army knife.
๐ŸŒ
Laravel Daily
laraveldaily.com โ€บ post โ€บ php-check-for-empty-values-not-is-null-vs-isset
PHP Check for Empty Values: "!" vs "is_null" vs "isset"
November 7, 2023 - As you can see, using the ! operation, both integer 0 and string '0' values are considered equal to false, after negation, they become true, which is not what we intended to do. When checking strings or integers for null values, it is better to avoid loose operations and use strict === null or is_null checks are equivalent.
๐ŸŒ
Reddit
reddit.com โ€บ r/php โ€บ empty, is_null, isset comparisons
r/PHP on Reddit: empty, is_null, isset comparisons
August 24, 2023 -

So I saw this answer on Stackoverflow comparing empty() vs is_null() vs isset():

Is his example at the bottom still the best way in modern PHP?

 โ€œfooโ€    โ€œโ€    0 FALSE NULL undefined
empty() FALSE TRUE TRUE TRUE TRUE TRUE
is_null() FALSE FALSE FALSE FALSE TRUE TRUE  (ERROR)
isset() TRUE TRUE TRUE TRUE FALSE FALSE

If you want to check if there's any value other than null or undefined, use isset($var)*(because* !is_null() generates a warning on undefined variables.)

If you want to check if the value is non-blank text or any number including zero, it gets trickier:

if (!empty($v) || (isset($v) && ($v === 0 || $v === '0'))) {
    // $v is non-blank text, true, 0 or '0'
    // $v is NOT an empty string, null, false or undefined
}
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-null
PHP NULL - GeeksforGeeks
July 12, 2024 - Variables declared without an initial value or unset are automatically assigned NULL. ... Example: The example shows the basic implementation of PHP NULL datatype. ... Example 2: The following example uses PHP is_null() function to check if ...
๐ŸŒ
Amit Merchant
amitmerchant.com โ€บ all-the-ways-to-handle-null-values-in-php
All the ways to handle null values in PHP - Amit Merchant
September 6, 2023 - It returns the value of the variable if itโ€™s not null and returns the value of the second operand if the variable is null. The operator was introduced in PHP 7.0.
๐ŸŒ
w3resource
w3resource.com โ€บ php โ€บ function-reference โ€บ is_null.php
PHP is_null() function - w3resource
The is_null () function is used to test whether a variable is NULL or not. ... Returns TRUE if var_name is null, FALSE otherwise. ... <?php $var_name = TRUE; if (is_null($var_name)) { echo 'Variable is NULL'; } else { echo 'Variable is not NULL'; ...
๐ŸŒ
Virendra's TechTalk
virendrachandak.com โ€บ techtalk โ€บ web development โ€บ php isset() vs empty() vs is_null()
PHP isset() vs empty() vs is_null() - Virendra's TechTalk
December 17, 2025 - I have tested the above values ... โ€œโ€? Thanks BTW.. ... A variable is NULL if it has no value, and points to nowhere in memory. empty() is more a literal meaning of empty, e.g. the string โ€œโ€ is empty, but is not NULL....
๐ŸŒ
DEV Community
dev.to โ€บ salmazz โ€บ php-check-for-empty-values-vs-isnull-vs-isset-vs-isempty-46k4
Decoding PHP's Empty Value Functions: When to Use Which - DEV Community
November 10, 2023 - The is_null() function checks if a variable is exactly NULL. It does not check for any other "empty" values like '' (empty string) or 0 (zero as an integer or float), it only returns true if the variable is NULL.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-empty-and-null-in-PHP
What is the difference between empty() and null() in PHP? - Quora
The above codes checks if $my_connection variable is not active in memory, if yes, then create a new connection using CreateConnection's function otherwise return the active connection. The difference between empty and is_null() is that, the empty() checks and returns true if a string is created in memory but empty while is_null() checks and return true if a variable is currently created and itโ€™s value not available in memory.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_is_null_function.htm
PHP Variable Handling is_null() Function
First introduced in core PHP 4.0.4, the is_null() function continues to function easily in PHP 5, PHP 7, and PHP 8. This program uses the PHP Variable Handling is_null() function to check whether a variable is null.
๐ŸŒ
Front Line PHP
front-line-php.com โ€บ dealing-with-null
Dealing with null - Front Line PHP
As a side note: take a look at the nullable notation: we've prefixed Date with a question mark, indicating that it can either be Date or null. We've also added a default = null value, to make sure the value is never uninitialized; you know, to prevent all those runtime errors you might encounter. Back to our example: what if we want to do something with our payment date's timestamp:
๐ŸŒ
DEV Community
dev.to โ€บ hurayraiit โ€บ understanding-php-types-null-40g7
Understanding PHP Types - NULL - DEV Community
October 22, 2024 - null is case-insensitive, so null, NULL, and NuLl are all treated the same. It is the only possible value of the null type. The is_null() function can be used to check whether a variable is null.
Top answer
1 of 16
247

It's language specific, but in PHP :

Null means "nothing". The var has not been initialized.

False means "not true in a boolean context". Used to explicitly show you are dealing with logical issues.

0 is an int. Nothing to do with the rest above, used for mathematics.

Now, what is tricky, it's that in dynamic languages like PHP, all of them have a value in a boolean context, which (in PHP) is False.

If you test it with ==, it's testing the boolean value, so you will get equality. If you test it with ===, it will test the type, and you will get inequality.

So why are they useful ?

Well, look at the strrpos() function. It returns False if it did not find anything, but 0 if it has found something at the beginning of the string!

<?php
// pitfall :
if (strrpos("Hello World", "Hello")) { 
    // never exectuted
}

// smart move :
if (strrpos("Hello World", "Hello") !== False) {
    // that works !
}
?>

And of course, if you deal with states, you want to make a difference between the following:

  • DebugMode = False (set to off)
  • DebugMode = True (set to on)
  • DebugMode = Null (not set at all; will lead to hard debugging ;-))
2 of 16
54

null is null. false is false. Sad but true.

there's not much consistency in PHP (though it is improving on latest releases, there's too much backward compatibility). Despite the design wishing some consistency (outlined in the selected answer here), it all get confusing when you consider method returns that use false/null in not-so-easy to reason ways.

You will often see null being used when they are already using false for something. e.g. filter_input(). They return false if the variable fails the filter, and null if the variable does not exists (does not existing means it also failed the filter?)

Methods returning false/null/string/etc interchangeably is a hack when the author care about the type of failure, for example, with filter_input() you can check for ===false or ===null if you care why the validation failed. But if you don't it might be a pitfall as one might forget to add the check for ===null if they only remembered to write the test case for ===false. And most php unit test/coverage tools will not call your attention for the missing, untested code path!

Lastly, here's some fun with type juggling. not even including arrays or objects.

var_dump( 0<0 );        #bool(false)
var_dump( 1<0 );        #bool(false)
var_dump( -1<0 );       #bool(true)
var_dump( false<0 );    #bool(false)
var_dump( null<0 );     #bool(false)
var_dump( ''<0 );       #bool(false)
var_dump( 'a'<0 );      #bool(false)
echo "\n";
var_dump( !0 );        #bool(true)
var_dump( !1 );        #bool(false)
var_dump( !-1 );       #bool(false)
var_dump( !false );    #bool(true)
var_dump( !null );     #bool(true)
var_dump( !'' );       #bool(true)
var_dump( !'a' );      #bool(false)
echo "\n";
var_dump( false == 0 );        #bool(true)
var_dump( false == 1 );        #bool(false)
var_dump( false == -1 );       #bool(false)
var_dump( false == false );    #bool(true)
var_dump( false == null );     #bool(true)
var_dump( false == '' );       #bool(true)
var_dump( false == 'a' );      #bool(false)
echo "\n";
var_dump( null == 0 );        #bool(true)
var_dump( null == 1 );        #bool(false)
var_dump( null == -1 );       #bool(false)
var_dump( null == false );    #bool(true)
var_dump( null == null );     #bool(true)
var_dump( null == '' );       #bool(true)
var_dump( null == 'a' );      #bool(false)
echo "\n";
$a=0; var_dump( empty($a) );        #bool(true)
$a=1; var_dump( empty($a) );        #bool(false)
$a=-1; var_dump( empty($a) );       #bool(false)
$a=false; var_dump( empty($a) );    #bool(true)
$a=null; var_dump( empty($a) );     #bool(true)
$a=''; var_dump( empty($a) );       #bool(true)
$a='a'; var_dump( empty($a));      # bool(false)
echo "\n"; #new block suggested by @thehpi
var_dump( null < -1 ); #bool(true)
var_dump( null < 0 ); #bool(false)
var_dump( null < 1 ); #bool(true)
var_dump( -1 > true ); #bool(false)
var_dump( 0 > true ); #bool(false)
var_dump( 1 > true ); #bool(true)
var_dump( -1 > false ); #bool(true)
var_dump( 0 > false ); #bool(false)
var_dump( 1 > true ); #bool(true)