DSGameMaker Posted November 30, 2008 Share Posted November 30, 2008 Which is more efficient: if (strlen($name) == 0) { echo "No name"; } if ($name == "") { echo "No name"; } Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 30, 2008 Share Posted November 30, 2008 Probably the second, but unless you're going to loop this thousands of times, the difference will be negligible. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 30, 2008 Share Posted November 30, 2008 inside a loop that ran 1,000,000 times, running on my mid-range pc, the difference was about 0.2 seconds with ($name == "") being that faster one. aka...not a real difference. on a similar note...(empty($name)) was faster by a hair Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 I would say that the top one is a better way of checking it though, but that is just personal preference. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 Just as a tip, you'd want to use trim() before checking so that someone doesn't enter only space characters. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 The best way, in my opinion, is <?php function check_len($check) { $check = str_replace(" ","",$check); $check = trim($check); $check = strlen($check); return $check; } if (check_len($name) == 0) { echo "No name"; } ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 The str_replace() is pretty useless because trim() would take care of it anyway. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 When i have used trim if i enter a few spaces it doesn't work. Unless it has been updated in PHP 5 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 <?php echo strlen(trim(" ")) . "\n"; ?> Says 0 for me. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 I didn't say it doesn't work, i said when i have used it before it didn't work. Although i think i use to do if(trim($name) == "") { } Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 That should still work. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 :s i dunno then,lol, i just no i couldn't get it to work properly when learning php so i now use that. Quote Link to comment Share on other sites More sharing options...
DSGameMaker Posted November 30, 2008 Author Share Posted November 30, 2008 Wow, didn't expect this much discussion. I'm going to continue using (strlen($name) == 0) I seem to beleive that working with a string uses more memory than a number, and that PHP has to free up a chunk to hold what could be 200 different ASCII characters, when actually, the string is empty. I bet I am wrong, but I don't know. It's good to see the difference is only minimal though Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 When you work with a string literal like that in a boolean statement, it won't actually allocate more than the length that you actually provide. When you say "", it doesn't actually allocate any new space other than whatever data it has to give to the yylval in Flex and Bison. The thing is, you should use trim() regardless. Then you'd have: strlen(trim($name)) And two function calls are much heavier than 1. Quote Link to comment Share on other sites More sharing options...
DSGameMaker Posted December 1, 2008 Author Share Posted December 1, 2008 Thanks everyone! Topic Solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.