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 OverflowNull 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';
?>';
Use empty(). It checks for both empty strings and null.
if (!empty($_POST['user'])) {
// do stuff
}
From the manual:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
empty, is_null, isset comparisons
Difference between NULL and null in PHP - Stack Overflow
Null vs. False vs. 0 in PHP - Stack Overflow
Better way to check if values are empty?
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.โ
How do I set a variable to null in PHP?
$variable = null;
Alternatively, you can use the unset() function to destroy the variable, which will also set it to null:โ
unset($variable);How can I check if a variable is null in PHP?
$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";
}
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
}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 ;-))
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)