34

I'm trying to write a script that when a user uploads a file and does not enter a name an error is returned. I've tried using is_null, empty, and isset and they all do not work. Eg, below, is_null returns an error even when a name is entered. Can anyone help?

$caption = $_REQUEST[$name_input_name];

if(is_null($caption)) {
    $file->error = 'Please Enter a Title';
    return false;
}
3
  • 2
    FWIW, for anyone else reading this question, if all three of is_null($caption), empty($caption), and !isset($caption) proceed to the code which sets $file->error, than that means $caption is null -- which could have been discovered by doing var_dump($caption). So this question isn't really about the difference between these three -- though the answers are useful in explaining the difference. Aug 17, 2016 at 18:41
  • 1
    Also for the love of god please use POST or GET and not BOTH unless you explicitly have to.
    – brandito
    Dec 11, 2017 at 22:30

14 Answers 14

28

is_null() emits a WARNING if variable is not set, but isset() and empty() don't.

$a - variable with not null value (e.g. TRUE)
$b - variable with null value. `$b = null;`
$c - not declared variable
$d - variable with value that cast to FALSE (e.g. empty string, FALSE or empty array)
$e - variable declared, but without any value assigned
$a->a - declared, but not assigned object property. (`public $a;`)
A::$a - declared, but not assigned static class property.

         |   $a  |   $b  |   $c  |   $d  |   $e  | $a->a | A::$a |
---------+-------+-------+-------+-------+-------+-------+-------+
is_null()| FALSE | TRUE  |TRUE*W | FALSE | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
isset()  | TRUE  | FALSE | FALSE | TRUE  | FALSE | FALSE | FALSE |
---------+-------+-------+-------+-------+-------+-------+-------+
empty()  | FALSE | TRUE  | TRUE  | TRUE  | TRUE  | TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
null === | FALSE | TRUE  |TRUE*W | FALSE | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
null ==  | FALSE | TRUE  |TRUE*W | TRUE  | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+

TRUE*W - function return TRUE, but same time emits WARNING.

On empty() function documentation page you can read, that:

The following things are considered to be empty:

....

$var; (a variable declared, but without a value)

It can be misleading that code $var; is defining a variable, but does not assign any value to it, but it is wrong. Variable $var is still undefined and type recognize functions, like is_null() emits warnings if you pass $var as an argument.

But it is not right for unsettled class or object properties. Declaring them without assigning some value automatically assigns NULL.

UPD Typed properties in PHP 7.4 DO NOT assigned by NULL by default. If you does not set any value to them, they are considered as unassigned.

Some low level descriptions:

isset() and empty() are core functions, that will be compiled directly to specific opcode according to zval type:

ZEND_ISSET_ISEMPTY_THIS
ZEND_ISSET_ISEMPTY_CV
ZEND_ISSET_ISEMPTY_VAR
ZEND_ISSET_ISEMPTY_DIM_OBJ
ZEND_ISSET_ISEMPTY_PROP_OBJ
ZEND_ISSET_ISEMPTY_STATIC_PROP

Furthermore they will compile by the same function zend_compile_isset_or_empty

Function is_null() is type recognizer function, like is_numeric, is_recource, is_bool, etc. And will be called like user-defined function with opcodes INIT_FCALL_BY_NAME/DO_FCALL_BY_NAME and so.

/* {{{ proto bool is_null(mixed var)
   Returns true if variable is null
   Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
PHP_FUNCTION(is_null)
{
    php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
}
17

isset() will check if the variable is set, ie

<?php

echo isset($var); // false

$var = 'hello';

empty() will check if the variable is empty, ie

<?php

$emptyString = '';

echo empty($emptyString); // true

is_null() will check for NULL which is different from empty, because it's set to NULL not an empty string. (NULL might be a confusing concept)

Since your title is a string, I think you want to be using empty()

if (!isset($_REQUEST[$name_input_name]) || empty($_REQUEST[$name_input_name])) {
    $file->error = 'Please Enter a Title';
    return false;
}
7
  • that code you suggest still returns an error, regardless if I enter a title or not. Sep 11, 2012 at 18:48
  • I suspect there is something else in your application causing a problem. Is error reporting on? Sep 11, 2012 at 18:48
  • "empty() will check if the variable is empty," - not true. php.net/manual/en/types.comparisons.php Dec 26, 2012 at 20:28
  • 8
    i think there could be better explanation than isset() check is set, empty check is empty and is_null check null.
    – Ali Faizan
    Sep 18, 2013 at 9:42
  • Notice: To check is $arr['key'] === null - always first check isset() then is_null(). Nov 11, 2014 at 14:00
15

PHP empty() vs is_null() vs isset():

   “”  “foo” NULL FALSE    0 undefined
empty() TRUE FALSE TRUE TRUE TRUE TRUE
is_null() FALSE FALSE TRUE FALSE FALSE TRUE  (ERROR)
isset() TRUE TRUE FALSE TRUE TRUE 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
}
8

isset()

From PHP manual – isset():

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty()

From PHP Manual – empty():

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

is_null()

From PHP Manual – is_null():

is_null — Finds whether a variable is NULL

In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.

1
5

I think you meant to use isset before you assigned it to something:

if(!isset($_REQUEST[$name_input_name]))
{
    $file->error = 'Please Enter a Title';
    return false;
}

$caption = $_REQUEST[$name_input_name];
8
  • Agreed, I would stay away from $_REQUEST also, make it specific to the situation.
    – wesside
    Sep 11, 2012 at 18:38
  • @minitech That doesn't work. When I enter a name, it still returns with the error, please enter a title. Sep 11, 2012 at 18:40
  • @wes, what should I use instead of $_REQUEST? Sep 11, 2012 at 18:40
  • Ok...do an isset with $name_input_name also. Is it a $_GET or a $_POST, you should know what it is. I don't like it because it includes $_COOKIE, which isn't a submission parameter, just lumps everything together.
    – wesside
    Sep 11, 2012 at 18:41
  • @user1605871: Wait... did you mean 'name_input_name' instead of $name_input_name? i.e. is your field called name_input_name, or do you have a variable called $name_input_name?
    – Ry-
    Sep 11, 2012 at 18:42
4

Here is a very good explanation:

enter image description here

4

Not so easy to get the difference between isset(), is_null() and empty(). It is more subtle than we would like to believe.

Since, it is not easy to clearly explain the difference with words, I suggest to look at this page : there is a detailed table containing behavior for each function. It clearly and fully explains the differences.

By the way, I'm truly convinced than many PHP scripts contain security breach due to the misunderstood of the behaviors.

0
1

I use strlen to count the number of characters

 if (strlen($_REQUEST['name_input_name']) < 1) {
    $file->error = 'Please Enter a Title';
    return false;
}
1

You can try this :

if(!isset($_REQUEST['name_input_name']))
{
    $file->error = 'Please Enter a Title';
    return false;
}
$caption = $_REQUEST['name_input_name'];

Note : $_REQUEST is an Global array that store the data in key=>value pair. consider "name_input_name" as value extracted from server.

if name_input_name is set to some value code will skip if block and store the value to variable $caption.

1

I wrote this php page that hopefully will be of help to you. If you run it, it shows the interaction of these particular types of variables with these functions (plus one I made for myself), and also it compares them with each other with the == operator: https://pastebin.com/whPFMams

<!doctype html>
<html>
<head>
    <style>
    td.first{
        font-weight: bold;
    }
    h3 {
        text-align: center;
    }
    </style>
</head>
<body>
    <h3>FUNCTIONS COMPARISON</h3><br>
<pre>
<h4>Variables</h4>
class obj{};
$n;

Array:
"a" => -1,
"b" => 0,
"c" => 0.0,
"d" => '',
"e" => ' ',
"f" => '0',
"g" => "\0",
"h" => true,
"i" => false,
"j" => null,
"k" => array(), 
"l" => new obj(),
"m" => new stdClass(),
"n" => $n (undefined)

z = (undeclared)

<h4>My function</h4>
function isvalid($value,$strict=false){
    if( !is_null($value) && value!==false && (!empty($value) || $value == 0) ){
        if($strict)
            {
                if(is_bool($value) || is_array($value) || is_object($value))
                    {return false;}
                else
                    {return true;}
            }
        else
            {return true;}
    }
    else
        {return false;}
}


<h4>Var_dump</h4>
<?php
error_reporting(E_ERROR | E_PARSE);

class obj{};

function isvalid($value,$strict=false){
    if( !is_null($value) && $value!==false && (!empty($value) || $value == 0) ){
        if($strict)
            {
                if(is_bool($value) || is_array($value) || is_object($value))
                    {return false;}
                else
                    {return true;}
            }
        else
            {return true;}
    }
    else
        {return false;}
}


$n;
$values = [
"a" => -1,
"b" => 0,
"c" => 0.0,
"d" => '',
"e" => ' ',
"f" => '0',
"g" => "\0",
"h" => true,
"i" => false,
"j" => null,
"k" => array(), 
"l" => new obj(),
"m" => new stdClass(),
"n" => $n
//"z"
];


echo var_dump($values)."\n z = "; echo var_dump($z)."\n";

?>
</pre><br />
<table border = "1">
         <tr>
            <th>== and other</th>
            <?php 
            echo "<th>"; echo var_dump($values["a"]); echo "<br> : a = -1 </th>";
            echo "<th>"; echo var_dump($values["b"]); echo "<br> : b = 0 </th>";
            echo "<th>"; echo var_dump($values["c"]); echo "<br> : c = 0.0 </th>";
            echo "<th>"; echo var_dump($values["d"]); echo "<br> : d = '' </th>";
            echo "<th>"; echo var_dump($values["e"]); echo "<br> : e = ' ' </th>";
            echo "<th>"; echo var_dump($values["f"]); echo "<br> : f = '0' </th>";
            echo "<th>"; echo var_dump($values["g"]); echo "<br> : g = '/0' </th>";
            echo "<th>"; echo var_dump($values["h"]); echo "<br> : h = true </th>";
            echo "<th>"; echo var_dump($values["i"]); echo "<br> : i = false </th>";
            echo "<th>"; echo var_dump($values["j"]); echo "<br> : j = null </th>";
            echo "<th>"; echo var_dump($values["k"]); echo "<br> : k = empty array </th>";
            echo "<th>"; echo var_dump($values["l"]); echo "<br> : l = empty object (Notices with numbers)</th>";
            echo "<th>"; echo var_dump($values["m"]); echo "<br> : m = empty stdClass (Notices with numbers)</th>";
            echo "<th>"; echo var_dump($values["n"]); echo "<br> : n = undefined </th>";
            echo "<th>"; echo var_dump($z); echo "<br> : z = undeclared (Notices)</th>"; 
            ?>
         </tr>
         <?php
         foreach ($values as $key => $val){
            echo '
            <tr>
            <td class="first">'; echo var_dump($val); echo '<br> : '.$key.'</td>';
            foreach ($values as $key2 => $val2){
                echo '<td>'; echo $val==$val2 ? var_dump($val==$val2) : /*var_dump($val==$val2).*/''; echo'</td>';
            }
            echo '<td>'; echo $val==$z ? var_dump($val==$z) : /*var_dump($val==$z).*/''; echo'</td>';
            echo '</tr>';
         }
         //z
         echo '
            <tr>
            <td class="first">'; echo var_dump($z); echo '<br> : z (Notices)</td>';
            foreach ($values as $key2 => $val2){
                echo '<td>'; echo $z==$val2 ? var_dump($z==$val2) : /*var_dump($z==$val2).*/''; echo'</td>';
            }
            echo '<td>'; echo $z==$z ? var_dump($z==$z) : /*var_dump($z==$z).*/''; echo'</td>';
            echo '</tr>';
            
          //!empty || ==0
         echo '
            <tr>
            <td class="first"> !empty || ==0 </td>';
            foreach ($values as $key => $val){
                echo '<td>'; echo !empty($val) || $val==0 ? var_dump(!empty($val) || $val==0) : /*var_dump(!empty($val) || $val==0.*/''; echo'</td>';
            }
            echo '<td>'; echo !empty($z) || $z==0 ? var_dump(!empty($z) || $z==0) : /*var_dump(!empty($z) || $z==0).*/''; echo'</td>';
            echo '</tr>';                            
         
         //isvalid
         echo '
            <tr>
            <td class="first"> isvalid() </td>';
            foreach ($values as $key => $val){
                echo '<td>'; echo isvalid($val) ? var_dump(isvalid($val)) : /*var_dump(isvalid($val)).*/''; echo'</td>';
            }
            echo '<td>'; echo isvalid($z) ? var_dump(isvalid($z)) : /*var_dump(isvalid($z)).*/''; echo'</td>';
            echo '</tr>';  
            
         //isvalid strict
         echo '
            <tr>
            <td class="first"> isvalid(strict) </td>';
            foreach ($values as $key => $val){
                echo '<td>'; echo isvalid($val,true) ? var_dump(isvalid($val,true)) : /*var_dump(isvalid($val,true)).*/''; echo'</td>';
            }
            echo '<td>'; echo isvalid($z,true) ? var_dump(isvalid($z,true)) : /*var_dump(isvalid($z,true)).*/''; echo'</td>';
            echo '</tr>'; 
            
         //!is_null
         echo '
            <tr>
            <td class="first"> !is_null </td>';
            foreach ($values as $key => $val){
                echo '<td>'; echo !is_null($val) ? var_dump(!is_null($val)) : /*var_dump(!is_null($val)).*/''; echo'</td>';
            }
            echo '<td>'; echo !is_null($z) ? var_dump(!is_null($z)) : /*var_dump(!is_null($z)).*/''; echo'</td>';
            echo '</tr>';     
            
         //isset
         echo '
            <tr>
            <td class="first"> isset (No Notices)</td>';
            foreach ($values as $key => $val){
                echo '<td>'; echo isset($val) ? var_dump(isset($val)) : /*var_dump(isset($val)).*/''; echo'</td>';
            }
            echo '<td>'; echo isset($z) ? var_dump(isset($z)) : /*var_dump(isset($z)).*/''; echo'</td>';
            echo '</tr>';  
            
         //!empty
         echo '
            <tr>
            <td class="first"> !empty (No Notices) </td>';
            foreach ($values as $key => $val){
                echo '<td>'; echo !empty($val) ? var_dump(!empty($val)) : /*var_dump(!empty($val)).*/''; echo'</td>';
            }
            echo '<td>'; echo !empty($z) ? var_dump(!empty($z)) : /*var_dump(!empty($z)).*/''; echo'</td>';
            echo '</tr>';   
            
         // if
         echo '
            <tr>
            <td class="first"> if </td>';
            foreach ($values as $key => $val){
                echo '<td>'; echo $val ? var_dump(true) : /*var_dump(false).*/''; echo'</td>';
            }
            echo '<td>'; echo $z ? var_dump(true) : /*var_dump(false).*/''; echo'</td>';
            echo '</tr>';      
            
         ?>
         <tr>
            <th>functions</th>
            <?php 
            echo "<th>"; echo var_dump($values["a"]); echo "<br> : a = -1 </th>";
            echo "<th>"; echo var_dump($values["b"]); echo "<br> : b = 0 </th>";
            echo "<th>"; echo var_dump($values["c"]); echo "<br> : c = 0.0 </th>";
            echo "<th>"; echo var_dump($values["d"]); echo "<br> : d = '' </th>";
            echo "<th>"; echo var_dump($values["e"]); echo "<br> : e = ' ' </th>";
            echo "<th>"; echo var_dump($values["f"]); echo "<br> : f = '0' </th>";
            echo "<th>"; echo var_dump($values["g"]); echo "<br> : g = '/0' </th>";
            echo "<th>"; echo var_dump($values["h"]); echo "<br> : h = true </th>";
            echo "<th>"; echo var_dump($values["i"]); echo "<br> : i = false </th>";
            echo "<th>"; echo var_dump($values["j"]); echo "<br> : j = null </th>";
            echo "<th>"; echo var_dump($values["k"]); echo "<br> : k = empty array </th>";
            echo "<th>"; echo var_dump($values["l"]); echo "<br> : l = empty object (Notices with numbers)</th>";
            echo "<th>"; echo var_dump($values["m"]); echo "<br> : m = empty stdClass (Notices with numbers)</th>";
            echo "<th>"; echo var_dump($values["n"]); echo "<br> : n = undefined </th>";
            echo "<th>"; echo var_dump($z); echo "<br> : z = undeclared (Notices)</th>"; 
            ?>
         </tr>
      </table>
    <br>
    (Notices) : This line is full of Notices. <br>
    (Notices with numbers) : This line has Notices about number types interactions. <br>
    (No Notices) : This line has absolutely no Notices.
</body>
</html>
1
  • 1
    Welcome to SO and thank you for answering a question :) However, please leave some explanation as to what you did and why, so the OP can better understand it. Jul 20, 2020 at 10:22
0

is_null is the dual of isset except isset does not print notices if the variable is null.

>$ciao;
>var_export(is_null($ciao));
>PHP Notice:  Undefined variable: ciao in php shell code on line 1
>true
>var_export(!isset($ciao));
>true
0

Ref this: different between isset, is_null, empty in PHP

  • isset() mean $var is defined and not null
  • is_null() mean $var is defined and null. throw an error if it is undefined.
  • empty() is anything mean NO => false, 0, 0.0, "", "0", null, array()
-1
  1. isset() — Determine if a variable is set and not NULL

  2. empty() - Determine if a variable is empty.

  3. is_null() - Determine if a variable is null

-2

In these php functions empty(),isset() & is_null are mainly used to test the variables.but each functions have different functionalists.

Credits : empty() , isset(), is_null()

1
  • 2
    Not an answer, rather a comment.
    – Daryl Gill
    Oct 20, 2014 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.