Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isset vs empty vs is_null

Tags:

php

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; } 
like image 619
user1605871 Avatar asked Sep 11 '12 18:09

user1605871


People also ask

Is empty or Isset better?

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.

What is the opposite of Isset?

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.

What is NULL and empty in PHP?

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)

Does PHP empty check Isset?

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.


2 Answers

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); } 
like image 149
rjhdby Avatar answered Sep 22 '22 11:09

rjhdby


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; } 
like image 25
Steve Robbins Avatar answered Sep 23 '22 11:09

Steve Robbins