Is There A Difference Between strlen()==0 and empty()?

red4d picture red4d · Aug 16, 2011 · Viewed 10.7k times · Source

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?

Answer

Paul picture Paul · Aug 16, 2011

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.