isset vs empty vs is_null

后端 未结 14 2142
野的像风
野的像风 2020-11-29 08: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

相关标签:
14条回答
  • 2020-11-29 08:40

    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()
    0 讨论(0)
  • 2020-11-29 08:46

    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.

    0 讨论(0)
  • 2020-11-29 08:48

    Here is a very good explanation:

    0 讨论(0)
  • 2020-11-29 08:49

    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 讨论(0)
  • 2020-11-29 08:49
    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

    0 讨论(0)
  • 2020-11-29 08:50

    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.

    0 讨论(0)
提交回复
热议问题