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; }
The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not. The isset() function will generate a warning or e-notice when the variable does not exists. The empty() function will not generate any warning or e-notice when the variable does not exists.
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.
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)
The isset() and ! empty() functions are similar and both will return the same results. But the only difference is ! empty() function will not generate any warning or e-notice when the variable does not exists.
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); }
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With