Test for Empty String ?

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
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Test for Empty String ?

Post by HUWUWA »

Hi guys, should I do this to test for an empty string:

if(strlen($strMyString) == 0)...

or this to avoid a function call:

if($strMyString == "")...

? The later seems more efficient but does it really test for an empty string ? Maybe there is an even better method ? Thanks.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I've always used the latter, but I'm trying to change to the

Code: Select all

if (""==$strMyString))
flipped version as it seems to be gaining favor in the community and does help catch the common assignment instead of comparision bug. I can't think of a case in PHP where your two versions would give different results.
User avatar
theChosen
Forum Newbie
Posts: 15
Joined: Sun Aug 18, 2002 11:00 am
Location: RO, Europe

Post by theChosen »

What about
if (!$strMyString) echo '$strMyString is empty';
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

1) that breaks with $strMyString = 0; (zero is equivalent to false (but of a different type, so === will return the correct answer) in PHP)

2) it's not generally good practice to use Hungarian Notation in PHP (the str in strMyString. it's ugly and not useful particularly in a loosely typed language)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I disagree with 2) - not completely
but sometimes it's quite useful i.e. interfacing if you want to make clear how you will treat a parameter
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Code: Select all

if (strlen(trim($string)) == 0) {
   echo "String is empty";
}
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Post by HUWUWA »

I'm just gonna use the empty() function; it seems the safest and most reliable. Thanks guys.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

just remember that

Code: Select all

<html><body>
<?php 
	$s = "0";
	print( empty($s)? 'is empty' : 'is not empty');
?>
</body></html>
will say 'is empty' because "0" is evaluated as empty
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Post by HUWUWA »

Oh, thanks, I didn't realize that. So if some guy enters "0" into a form textbox and I use empty() it will equate to TRUE.

Damn, so should I just use the if($textboxString == "") to check for an empty textbox ?

Or the if(strlen($textboxString) == 0) ?

This is to verify whether or not someone entered something into a form textbox. Both methods seem to work just fine.

Thanks guys.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I like protokol's strlen(trim()) combination
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Looks like volka is the best...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I have to compensate the times when I talk/write complete nonsense :D
Post Reply