php is null when empty?
What you're looking for is:
if($variable === NULL) {...}
Note the ===
.
When use ==
, as you did, PHP treats NULL, false, 0, the empty string, and empty arrays as equal.
How to validate null or empty in php
You can validate in many ways :
By using following php variable handling functions
is_null(mixed $var)
isset(mixed $var)
empty(mixed $var)
By using comparison operator
==
,===
or!=
is_null(mixed $var)
You can use php variable handling function is_null(mixed $var)
which returns TRUE if var is null, otherwise FALSE.
<?php
$foo = NULL;
$bar = 'NULL';
var_dump(is_null($foo), is_null($bar));
?>
OUTPUT
bool(true) bool(false)
As you can see $bar = 'NULL'
and $bar = NULL
both are different thing. Actually, in one it is initializing with a string, not with NULL.
isset(mixed $var)
It returns TRUE if var exists and has value other than NULL, FALSE otherwise.
Note
isset()
only works with variables as passing anything else will result in a parse error. For checking if constants are set use, the defined() function.
<?php
$foo = NULL;
$bar = 'NULL';
var_dump(isset($foo), isset($bar));
?>
OUTPUT
bool(false) bool(true)
empty(mixed $var)
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
<?php
$foo = NULL;
$bar = 'NULL';
var_dump(empty($foo), empty($bar));
?>
OUTPUT
bool(true) bool(false)
==
or ===
or !=
You can use comparison operator ==
, ===
or !=
to check whether a variable is null or not.
<?php
$foo = NULL;
$bar = 'NULL';
// Using == operator
echo "Using '==' operator\n";
if($foo == NULL)
echo "foo is NULL\n";
else
echo "foo is not NULL\n";
if($bar == NULL)
echo "bar is NULL\n";
else
echo "bar is not NULL\n";
// Using === operator
echo "\nUsing '===' operator\n";
if($foo === NULL)
echo "foo is NULL\n";
else
echo "foo is not NULL\n";
if($bar === NULL)
echo "bar is NULL\n";
else
echo "bar is not NULL\n";
?>
OUTPUT
Using '==' operator
foo is NULL
bar is not NULL
Using '===' operator
foo is NULL
bar is not NULL
The only difference between ==
and ===
is that ==
just checks to see if the left and right values are equal. But, the ===
operator (note the extra “=”) actually checks to see if the left and right values are equal and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).
Check for NULL or empty '' in function?
If you just check for the value of the variable you will get true whether null
or an empty string (''
):
null == ''; // 1 (true)
You also need to check for type:
null === ''; // (false)
So instead of:
if( $a != NULL )
Run:
if( $a !== NULL )
Which will also check for type; ''
is a string
- not null
type.
Rearranged function:
function title($a = NULL, $b = NULL, $c = NULL) {
if( $a !== NULL )
{
echo 'A1';
if( $a !== '' )
{
echo 'A2';
}
}
if( $b !== NULL )
{
echo 'B1';
if( $b !== '' )
{
echo 'B2';
}
}
if( $c !== NULL )
{
echo 'C1';
if( $c !== '' )
{
echo 'C2';
}
}
}
title(NULL, '', 'C'); // outputs: B1C1C2
Make a value into NULL instead of empty in php
It's a normal problem with null-able strings. For a quick solution, you can change your code to:
$code = NULL;
if( !empty($_FILES['logo']['name']) ){
...
}
For more details, you can check this question:
MySQL and PHP - insert NULL rather than empty string
php : How to check a field have blank/empty/NULL value?
doing strtotime will return false if it cannot convert to a time stamp.
$mo = strtotime($_POST['MondayOpen']);
if ($mo !== false)
{
//valid date was passed in and $mo is type int
}
else
{
//invalid date let the user know
}
What is the difference between null and empty?
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
.
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)
Source.
Example
$a
is NULL
.
$a = ''
is empty, but not NULL
.
Update
If
$a=''
is empty but notNULL
, when do I use theempty()
function and when do I use theisset()
function.
isset()
will return FALSE
is the variable is pointing to NULL
.
Use empty()
when you understand what is empty (look at the list above).
Also when you say it points nowhere in memory, what does that mean exactly?
It means that $str = ''
will be in memory as a string with length of 0.
If it were $str = NULL
, it would not occupy any memory.
PHP: insert NULL if input value is empty
You can try this way:
remove single invated comma from query and set them to query it will surely work for you
if (empty($_POST['caller'])){
$caller="NULL";
}else{
$caller=mysqli_real_escape_string($con, $_POST['caller']);
$caller="'$caller'";
}
$sql_caller = "INSERT INTO `tblcall_info` VALUES ('','$save_inc_id','$call_time','$call_date','$caller','$caller_contact','$receiver','$device')";
Related Topics
PHP Class Instantiation. to Use or Not to Use the Parentheses
PHP Mail() Function on Localhost
Setting the PHP Default Encoding to Utf-8
How to Check If an Array Contains a Specific Value in PHP
Checking If Customer Has Already Bought Something in Woocommerce
Truncate Text Containing Html, Ignoring Tags
Prevent Direct Access to File Called by Ajax Function
Simple Postgresql Statement - Column Name Does Not Exists
Can HTML Be Embedded Inside PHP "If" Statement
How to Validate Array in Laravel
PHP Replacing Multiple Spaces With a Single Space
Run a PHP File in a Cron Job Using Cpanel
What Is Autoloading; How to Use Spl_Autoload, _Autoload and Spl_Autoload_Register
Format Bytes to Kilobytes, Megabytes, Gigabytes
PHP 5 Disable Strict Standards Error
Disable PHP in Directory (Including All Sub-Directories) With .Htaccess