8

I was looking at some form validation code someone else had written and I saw this:

strlen() == 0

When testing to see if a form variable is empty I use the empty() function. Is one way better than the other? Are they functionally equivalent?

David
  • 4,027
  • 10
  • 50
  • 102
red4d
  • 277
  • 2
  • 4
  • 10

9 Answers9

9

strlen is to get the number of characters in a string while empty is used to test if a variable is empty

Meaning of empty:

empty("") //is empty for string
empty(0) // is empty for numeric types
empty(null) //is empty 
empty(false) //is empty for boolean
kazinix
  • 28,987
  • 33
  • 107
  • 157
  • What you (and a few others) said is exactly what I thought. I'd never seen anyone try to check if a variable was empty using `strlen` before, and I was doubtful their code was actually having the intended effect. Thanks everyone for the clarifications. – red4d Aug 16 '11 at 03:06
  • Why was this nicely readable (and accepted) answer only upvoted once? Is there a mistake in it? – mwfearnley Jun 10 '16 at 14:32
  • `if (!strlen($_POST['id'])) { //blah }` if i write condition like this what does it mean.? boolen or not.? @red4d @dpp – Mr world wide Oct 13 '17 at 11:03
  • I think the accepted answer should emphasize that the main difference is that when testing the "0" string, `strlen()==0` would return false while `empty()` would return true. Some people may read calling `empty()` on a string as equivalent to checking if it equals the empty string, which is wrong because of this error-prone edge case! – fbastien Oct 22 '19 at 12:44
9

There are a couple cases where they will have different behaviour:

empty('0'); // returns true, 
strlen('0'); // returns 1.

empty(array()); // returns true,
strlen(array()); // returns null with a Warning in PHP>=5.3 OR 5 with a Notice in PHP<5.3.

empty(0); // returns true,
strlen(0); // returns 1.
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
Paul
  • 139,544
  • 27
  • 275
  • 264
8

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)

strlen() simply check if the the string len is 0. It does not check for int, float etc. What is your situation.

reference

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
3

empty() will return true if $x = "0". So there is a difference.

http://docs.php.net/manual/en/types.comparisons.php

Kal
  • 24,724
  • 7
  • 65
  • 65
2
$f = 0; echo empty($f)? 'Empty':'Full'; // empty
$f = 0; echo strlen($f); // 1

For forms I use isset. It's more explicit. ;-)

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
2

empty is the opposite of boolean false.

empty is a language construct.

strlen is a function.

strlen returns the length of bytes of a string.

strlen($str)==0 is a comparison of the byte-length being 0 (loose comparison).

That comparison will result to true in case the string is empty - as would the expression of empty($str) do in case of an empty (zero-length) string, too.

However for everything else:

empty is the opposite of boolean false.

strlen returns the length of bytes of a string.

They don't share much with each other.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Hi hakre. Firstly, let me say I'm really wary of questioning a statement by someone with your rep, but did you mean to say that empty is the opposite of false? Surely true is the opposite of false, and empty is the same as false? Perhaps I'm getting old..! – Geoff Kendall Jun 05 '13 at 13:52
  • 1
    @GeoffKendall: Please see here: [PHP: Empty() is the Opposite of a Boolean Variable](http://hakre.wordpress.com/2011/04/02/php-empty-is-the-opposite-of-a-boolean-variable/) – hakre Jun 05 '13 at 17:02
1

The strlen way is almost perfect if you want to check if something is "empty as string", that is, when a single zero should NOT be counted as nothing but empty strings, empty arrays, null and false should all be considered no value. This is quite frequently needed.

So my recommendation would be:

    count($x)*strlen("$x")

This gives you:

  • 0 for false

  • 0 for null

  • 0 for "" (empty string)

  • 0 for [ ] (empty array), no warning

  • 1 for a numeric zero

  • 1 for "0" (a string zero)

Surely you can do empty($x) && "$x"!=="0" - it's more to the point but a little noisier, and surprisingly their time cost is nearly equal (under 50ms for a million iterations) so no reason to choose one over the other. Also, if you add !! (boolean cast) before strlen, you'll get a clear 0/1 answer for the question and sometimes it can be more convenient than true/false (debugging situations, switch, etc).

Also note that objects can't be checked like this. If there's a chance that an object comes along, go for the empty()-method. Objects are stubborn creatures.

dkellner
  • 8,726
  • 2
  • 49
  • 47
0

empty() is for all variables types. strlen(), I think, is better to use with strings or something that can be safely casted to strings. For examle,

strlen(array());

will throw PHP Warning: strlen() expects parameter 1 to be string, array given error

Timur
  • 6,668
  • 1
  • 28
  • 37
0

when the input is 0,

strlen(0) = 1
empty(0) = false <-- non-empty
Raptor
  • 53,206
  • 45
  • 230
  • 366