11

I recently discovered this interesting article by Deceze.
But I'm a bit confused by one of its advises:

never use empty or isset for variables that should exist

Using empty() is not good choice to test if $foo = ''; is empty?

Community
  • 1
  • 1
fralbo
  • 2,534
  • 4
  • 41
  • 73
  • Remember to accept the answer that answers your question. – Antony Thompson Nov 24 '16 at 12:02
  • Is there any situation in which `$foo` itself **may not exist**? Unless that's a legitimate possibility you want to account for, don't use `empty`; `== false` would do the same thing without disabling error reporting. – deceze Nov 24 '16 at 13:31

5 Answers5

8

You should not use empty (or isset) if you expect $foo to exist. That means, if according to your program logic, $foo should exist at this point:

if ($foo === '')

Then do not do any of these:

if (isset($foo))
if (empty($foo))

These two language constructs suppress error reporting for undefined variables. That's their only job. That means, if you use isset or empty gratuitously, PHP won't tell you about problems in your code. For example:

$foo = $bar;
if (empty($føø)) ...

Hmm, why is this always true, even when $bar contains the expected value? Because you mistyped the variable name and you're checking the value of an undefined variable. Write it like this instead to let PHP help you:

if (!$føø) ...

Notice: undefined variable føø on line ...

The condition itself is the same, == false (!) and empty produce the same outcome for the same values.

How exactly to check for an empty string depends on what values you do or don't accept. Perhaps $foo === '' or strlen($foo) == 0 is the check you're looking for to ensure you have a string with something in it.

deceze
  • 510,633
  • 85
  • 743
  • 889
7

What he means is if you want to check if the string is empty then empty won't do that. Empty can mean false, 0, null. Anything 'falsy'.

E.g. these are all true:

<?php

$string = null;
if (empty($string)) {
    echo "This is true";
}

$string = '';
if (empty($string)) {
    echo "This is true";
}

$string = 0;
if (empty($string)) {
    echo "This is true";
}

If you want to check if the string is an empty string you should do this check for '':

<?php 

$string = '';
if (isset($string) && $string === '') {
    echo "This is true";
}

$string = null;
if (isset($string) && $string === '') {
    echo "This is false";
}
joan16v
  • 5,055
  • 4
  • 49
  • 49
Antony Thompson
  • 1,608
  • 1
  • 13
  • 22
  • 2
    That's not actually what he means. He means that unless you're unsure whether **the variable itself** may or may not be set, don't use `empty` or `isset`. In all your examples the variable `$string` is guaranteed to exist when you're trying to use it, so the `isset` check (and by extension `empty`) is completely superfluous. How exactly to check "how empty" your string is is a different topic. `strlen` would be the best test for that. – deceze Nov 24 '16 at 13:29
  • @deceze answer is much better – Antony Thompson Nov 28 '16 at 22:31
  • Not only superfluous but actually harmful. Because isset and empty are just little brothers of @. – Your Common Sense Jul 21 '23 at 11:52
3

PHP's empty() can be used in many cases.

It works for checking:

  • if a string is blank

  • if a variable is undefined or null

And of course empty() is best for your case too.

joan16v
  • 5,055
  • 4
  • 49
  • 49
geekbro
  • 1,293
  • 12
  • 13
  • 2
    Also if a blank array - plus all those other "falsey" alternatives (`(int) 0`, etc). – ʰᵈˑ Nov 24 '16 at 11:29
  • `empty` does exactly the same thing as `== false`, except it doesn't throw any errors if the whole variable you're testing doesn't exist (`empty($undefined)` → no errors, `$undefined == false` → `Notice: undefined variable`). – deceze Nov 24 '16 at 13:51
  • On the contrary, anything but "best" – Your Common Sense Jul 21 '23 at 11:52
-1

Check String is empty or Not

<?php
     $test='';
     if(empty($test)){
          echo'It is empty';
     } else{
          echo'Its not empty';
     }
?>
Ram Chander
  • 1,088
  • 2
  • 18
  • 36
-2

Try using this php if function: $retVal = (condition) ? a : b ;

where condition: $value == null

a: is the value to display if $value is null

b: is the actual value to display or any other value to be displayed when $value is not null

In case of further guidance, kindly comment