tags:

views:

53

answers:

3

Hi,

Does empty() function validate all of this cases:

1.var=null

2.var=empty string ""/" "

3.var=not set

Example for validate use (Should I add some more code for validation or empty() enough?):

if(!empty($userName)){
  //some code
}

else{
  echo "not valid user name"
}

Edit: should isset() used before empty() or empty() include isset() case?

Thanks

+7  A: 

Yes, it does. It's in the php documentation.

Femaref
+1  A: 

If you want to check if the user has filled in anything at all then empty() is ok. If you, on the other hand, want to check if the user filled in a username longer than 3 chars (strlen()) or only with alphanumeric chars (preg_match()) you will need some more tools ;)

Leon
+4  A: 

Given the following i would say it fits your needs:

The following things are considered to be empty:

* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

EDIT:

should isset() used before empty() or empty() include isset() case?

This depends on the usage. IF you are in a position where you cant be sure $username exists then you need to use isset first to avoid an error about a nonexistent variable. If on the other hand you are sure $username will always exist then you can jsut test for the empty value and leave it at that.

prodigitalson