Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is empty() enough or use isset()?

Tags:

Note - I know pretty much about empty() and isset(), what I am asking is the most proper/efficient way of using them.

I've just saw at php.net this sentence under empty() reference:

No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

But again, as I'm mainly working now on other people's apps (either standalone projects, websites using cms or even frameworks) many many times I've seen people writing:

if (isset ($var) && $var != '') // or if (isset ($var) && !empty($var)) 

(mainly with $_GET and $_POST variables, but not only)

As far as I understand manual, snippet below is equivalent:

if (!empty($var)) 

Or am I missing something? What is technically the best way to check existense of variable with value?

I know both functions, their destinations. I just want to know if empty() is all needed to check if is variable set and given value. I know that it works, but is it 100% proper and safe?

like image 824
Forien Avatar asked Dec 09 '14 14:12

Forien


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.

Is NULL vs Isset?

PHP isset() vs.The is_null() function returns true if the value of a variable has been explicitly set to NULL . Otherwise, it simply returns false . On the other hand, isset() will return true as long as a variable is defined and its value is not NULL .

Should I use isset?

isset() is best for radios/checkboxes. Use empty() for strings/integer inputs. when a variable contains a value, using isset() will always be true. you set the variable yourself, so it's not a problem.

How do you know if it's not isset?

Answer: Use the PHP isset() function You can use the PHP isset() function to test whether a variable is set or not. The isset() will return FALSE if testing a variable that has been set to NULL .


1 Answers

Op codes generated from isset($var) && !empty($var)

line     #  *  op                           fetch           ext  return  operands ---------------------------------------------------------------------------------      3     0  >   EXT_STMT                                                           1      ISSET_ISEMPTY_VAR                      12800000    ~0       !0          2  >   JMPZ_EX                                      ~0    ~0,     ->6          3  >   ISSET_ISEMPTY_VAR                      11800000    ~1       !0          4      BOOL_NOT                                     ~2    ~1          5      BOOL                                         ~0    ~2          6  >   FREE                                               ~0    4     7  >   RETURN                                              1 

and from !empty($var)

line     #  *  op                           fetch           ext  return  operands ---------------------------------------------------------------------------------    3     0  >   EXT_STMT                                                           1      ISSET_ISEMPTY_VAR                      11800000    ~0       !0          2      BOOL_NOT                                           ~1       ~0          3      FREE                                                        ~1    4     4  >   RETURN                                                       1 

So yes it does repeat ISSET_ISEMPTY_VAR but !$empty. It depends on the value passed, but it it doesnt exist the top way is one opcode less BOOL_NOT. but if it does exist, the bottom way is quicker.

Though its unlikely this is going to be a bottleneck in any application

like image 191
exussum Avatar answered Oct 03 '22 13:10

exussum