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
All the ways to handle null values in PHP
Missed isset() and (for array entries) array_key_exists(). Be aware of the difference between "not set" and "set to null". isset() and the null coalesce operator do not differentiate, while many other methods will. Also behavior when calling functions or assigning properties may (depending on the type declaration) depend on whether strict types is enabled. More on reddit.com
๐ŸŒ r/PHP
18
17
September 6, 2023
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.
๐ŸŒ
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
}
๐ŸŒ
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.
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/php โ€บ all the ways to handle null values in php
r/PHP on Reddit: All the ways to handle null values in PHP
September 6, 2023 - Be aware of the difference between "not set" and "set to null". isset() and the null coalesce operator do not differentiate, while many other methods will. Also behavior when calling functions or assigning properties may (depending on the type declaration) depend on whether strict types is enabled. ... Will return true even if the value is null. ... Makes sense. The key exists and the value of the key doesn't matter.
๐ŸŒ
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.