Could somebody show me why this IF statement is failing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Could somebody show me why this IF statement is failing

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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

Post by volka »

You might want to use

Code: Select all

if ( 0<strlen(trim($row['product'])) ) {
instead
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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'])))
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply