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
Returns true if value is null, false otherwise. ... Micro optimization isn't worth it. You had to do it ten million times to notice a difference, a little more than 2 seconds $a===NULL; Took: 1.2424390316s is_null($a); Took: 3.70693397522s difference = 2.46449494362 difference/10,000,000 = 0.000000246449494362 The execution time difference between ===NULL and is_null is less than 250 nanoseconds.
🌐
W3Schools
w3schools.com › php › func_var_is_null.asp
PHP is_null() Function
<?php $a = 0; echo "a is " . is_null($a) . "<br>"; $b = null; echo "b is " . is_null($b) . "<br>"; $c = "null"; echo "c is " . is_null($c) . "<br>"; $d = NULL; echo "d is " . is_null($d) . "<br>"; ?> Try it Yourself » · The is_null() function ...
🌐
Educative
educative.io › answers › how-to-check-if-a-defined-variable-is-null-in-php
How to check if a defined variable is null in PHP
This method is supported by PHP 4 and later versions. Unlike the isset() method, the is_null method returns the following when we use it to check the variable: Returns true if the variable is null. Returns false if the variable is not null.
🌐
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"
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. In the same way, strictly not !== null and isset() statements are identical too.
🌐
Reddit
reddit.com › r/php › empty, is_null, isset comparisons
r/PHP on Reddit: empty, is_null, isset comparisons
May 21, 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
}
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-check-whether-a-variable-is-null-in-php.php
How to Check Whether a Variable is NULL in PHP
You can use the PHP is_null() function to check whether a variable is null or not. Let's check out an example to understand how this function works: Try this code » · <?php $var = NULL; // Testing the variable if(is_null($var)){ echo 'This ...
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
PHP isset() vs. empty() vs. is_null() | Envato Tuts+
June 11, 2021 - In that case, isset() will only return true if none of the passed values are NULL. 2. Don't use == to check if a value is NULL.
🌐
w3resource
w3resource.com › php › function-reference › is_null.php
PHP is_null() function - w3resource
(PHP 4 and above) Syntax: is_null (var_name) Parameter: *Mixed: Mixed indicates that a parameter may accept multiple (but not necessarily all) types. Return value: Returns TRUE if var_name is null, FALSE otherwise.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-check-whether-a-variable-is-null-in-php
How to check Whether a Variable is Null in PHP? - GeeksforGeeks
April 21, 2025 - We are given a variable and the task is to check whether the value of the given variable is null or not and returns a Boolean value using PHP. To check a variable is null or not, we use is_null() function. A variable is considered to be NULL ...
🌐
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › function.isset.html
Determine if a variable is set and is not NULL
<?php $a = array ('test' => 1, ... 'hello' equals NULL so is considered unset // If you want to check for NULL key values then try: var_dump(array_key_exists('hello', $a)); // TRUE // Checking deeper array values var_dump(isset($a['pie']['a'])); // TRUE var_dump(isset($a[...
🌐
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 - $var = null; if ($var === null) { echo "Variable is null"; } ... PHP has isset() the built-in function to check if the is set and not null.
🌐
Delft Stack
delftstack.com › home › howto › php › php notnull
Syntax to Check for Not Null and an Empty String in PHP | Delft Stack
March 11, 2025 - The negation operator (!) reverses the result, meaning if $variable is not null, the check continues to see if it is not an empty string using the strict comparison operator (!==). This ensures that we accurately identify valid data. This method is useful for scenarios where you want to ensure that a variable has been set and contains meaningful data. It’s a simple yet effective way to validate your PHP variables, especially when dealing with user input or data from external sources.
🌐
GeeksforGeeks
geeksforgeeks.org › php › why-to-check-both-isset-and-empty-function-in-php
Why to check both isset() and !empty() function in PHP ? - GeeksforGeeks
July 11, 2025 - In PHP, isset() checks if a variable is set and not null, while !empty() checks if a variable exists and is not empty (non-zero, non-null, non-false).
🌐
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 empty() function in PHP is used to determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value is one of the following:
Top answer
1 of 4
34

Rather than:

if (!($error == NULL))

Simply do:

if ($error)

One would think that the first is more clear, but it's actually more misleading. Here's why:

$error = null;

if (!($error == NULL)) {
    echo 'not null';
}

This works as expected. However, the next five values will have the same and (to many, unexpected) behavior:

$error = 0;
$error = array();
$error = false;
$error = '';
$error = 0.0;

The second conditional if ($error) makes it more clear that type casting is involved.

If the programmer wanted to require that the value actually be NULL, he should have used a strict comparison, i.e., if ($error !== NULL)

2 of 4
1

It is good to know exactly what is in your variable, especially if you are checking for uninitialized vs null or na vs true or false vs empty or 0.

Therefore, as mentioned by webbiedave, if checking for null, use

$error !== null
$error === null
is_null($error)

if checking for initilized, as shibly said

isset($var)

if checking for true or false, or 0, or empty string

$var === true
$var === 0
$var === ""

I only use empty for ''s and nulls since string functions tend to be inconsistent. If checking for empty

empty($var)
$var  // in a boolean context

// This does the same as above, but is less clear because you are 
// casting to false, which has the same values has empty, but perhaps
// may not one day.  It is also easier to search for bugs where you
// meant to use ===
$var == false

If semantically uninitialized is the same as one of the values above, then initialize the variable at the beginning to that value.

$var = ''
...  //some code

if ($var === '') blah blah.