Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isset() and empty() - what to use

Tags:

php

Could you help me to improve my coding style?:) In some tasks I need to check - is variable empty or contains something. To solve this task, I usually do the following.

Check - is this variable set or not? If it's set - I check - it's empty or not?

<?php     $var = '23';     if (isset($var)&&!empty($var)){         echo 'not empty';     }else{         echo 'is not set or empty';     } ?> 

And I have a question - should I use isset() before empty() - is it necessary? TIA!

like image 508
Dmitry Belaventsev Avatar asked Aug 25 '11 13:08

Dmitry Belaventsev


People also ask

What is the difference between isset () and empty ()?

The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL. The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.

Does Isset check for empty?

empty() function in PHP ? The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.

What can I use instead of isset?

The equivalent of isset($var) for a function return value is func() === null . isset basically does a !== null comparison, without throwing an error if the tested variable does not exist.

What is the use of isset ()?

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.


1 Answers

It depends what you are looking for, if you are just looking to see if it is empty just use empty as it checks whether it is set as well, if you want to know whether something is set or not use isset.

Empty checks if the variable is set and if it is it checks it for null, "", 0, etc

Isset just checks if is it set, it could be anything not null

With empty, the following things are considered 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)

From http://php.net/manual/en/function.empty.php


As mentioned in the comments the lack of warning is also important with empty()

PHP Manual says

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

Regarding isset

PHP Manual says

isset() will return FALSE if testing a variable that has been set to NULL


Your code would be fine as:

<?php     $var = '23';     if (!empty($var)){         echo 'not empty';     }else{         echo 'is not set or empty';     } ?> 

For example:

$var = "";  if(empty($var)) // true because "" is considered empty  {...} if(isset($var)) //true because var is set   {...}  if(empty($otherVar)) //true because $otherVar is null  {...} if(isset($otherVar)) //false because $otherVar is not set   {...} 
like image 174
Pez Cuckow Avatar answered Sep 19 '22 18:09

Pez Cuckow