Isset VS Empty VS Is_Null

isset vs empty vs 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;
}

In where shall I use isset() and !empty()

isset vs. !empty

FTA:

"isset() checks if a variable has a
value including (False, 0 or empty
string), but not NULL. Returns TRUE
if var exists; FALSE otherwise.

On the other hand the empty() function
checks if the variable has an empty
value empty string, 0, NULL or
False. Returns FALSE if var has a
non-empty and non-zero value."

What's the difference between 'isset()' and '!empty()' in PHP?

ISSET checks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUE for ISSET.

EMPTY checks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.

What is the difference between null and empty?

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)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

Source.


Example

$a is NULL.

$a = '' is empty, but not NULL.


Update

If $a='' is empty but not NULL, when do I use the empty() function and when do I use the isset() function.

isset() will return FALSE is the variable is pointing to NULL.

Use empty() when you understand what is empty (look at the list above).

Also when you say it points nowhere in memory, what does that mean exactly?

It means that $str = '' will be in memory as a string with length of 0.

If it were $str = NULL, it would not occupy any memory.

Why check both isset() and !empty()

This is completely redundant. empty is more or less shorthand for !isset($foo) || !$foo, and !empty is analogous to isset($foo) && $foo. I.e. empty does the reverse thing of isset plus an additional check for the truthiness of a value.

Or in other words, empty is the same as !$foo, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.

The manual puts it like this:

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

You can simply use !empty($vars[1]) here.



Related Topics



Leave a reply



Submit