Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty vs !empty 1

Status
Not open for further replies.

yamy

Technical User
Nov 22, 2002
54
US
which syntax is correct:

if (empty ($_POST['var'])) {

or

if (!empty ($_POST['var'])) {

and if both are correct, what determines the use of the leading !

thanks
amy
 
Hi Amy :)

if (empty($_POST['var'])) {
// means : if $_POST['var'] is empty ("" or 0) then do something

if (!empty($_POST['var'])) {
// means : if $_POST['var'] is not empty (either non "" or non 0) then do something

The leading ! means "function == FALSE".

Thus,
if (!empty($_POST['var'])) {
is the same as
if (empty($_POST['var']) == FALSE) {

 
Sleidia is absolutely correct, but empty is not a straightforward and behaves differently dependent on the type of variable that is being tested. read here for further info
 
Personally, I never use empty().

I prefer to check thing specifically, ie:

if($var != "") // for strings
if($var > 0) // for numbers
if(sizeof($var) > 0) // for arrays

I'm not saying that it's how it should be done though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top