Page 1 of 1

Could somebody show me why this IF statement is failing

Posted: Wed Jun 27, 2007 5:49 am
by impulse()

Code: Select all

if (!empty(trim($row['product']))) {
Am I right to assume that I'd have to make a separate variable and assign 'trim($row['product'])' to it and then do an !empty statement on that variable?

Re: Could somebody show me why this IF statement is failing

Posted: Wed Jun 27, 2007 5:55 am
by volka
You might want to use

Code: Select all

if ( 0<strlen(trim($row['product'])) ) {
instead

Posted: Wed Jun 27, 2007 5:55 am
by Gente
empty() takes a variable as parameter and trim returns a string.
But I don't think you should assign new variable try following

Code: Select all

if (trim($row['product']) == '')
or

Code: Select all

if (strlen(trim($row['product'])))

Posted: Wed Jun 27, 2007 5:59 am
by CoderGoblin
Empty()

Returns FALSE if var has a non-empty and non-zero value.

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)

So not just an empty string. This may be important if you are dealing with user responses where they may enter 0.