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
Ref this: different between isset, is_null, empty in PHP
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.
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.
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.
Here is a very good explanation:
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
isset() — Determine if a variable is set and not NULL
empty() - Determine if a variable is empty.
is_null() - Determine if a variable is null
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.