Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

strlen

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?

like image 891
red4d Avatar asked Aug 16 '11 02:08

red4d


2 Answers

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.
like image 78
Paul Avatar answered Sep 25 '22 00:09

Paul


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
like image 36
dpp Avatar answered Sep 23 '22 00:09

dpp