sandy1028 Posted April 13, 2012 Share Posted April 13, 2012 If the value of $keyword is empty, only then it should print "Hi". else if should print "Hello". Problem is sometimes the value $keyword will be "0". How to handle this condition $keyword=0; //$keyword='abc'; if (empty($keyword)) { echo 'Hi'; } else { echo "Hello"; } Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/ Share on other sites More sharing options...
Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 if (empty($keyword) && $keyword !== 0 ) { Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/#findComment-1336969 Share on other sites More sharing options...
MMDE Posted April 13, 2012 Share Posted April 13, 2012 Read documentation/api about the function empty. You could do this instead: if($keyword==''){ } But I guess you might want the functionality of !isset as well? if (empty($keyword) && $keyword !== 0 ) { ^ This won't work if you for example do this: $keyword = 0.0; Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/#findComment-1336970 Share on other sites More sharing options...
MMDE Posted April 13, 2012 Share Posted April 13, 2012 This seems to do exactly what you want... or at least I think so! o.o' if(!isset($keyword) || $keyword == '') Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/#findComment-1336973 Share on other sites More sharing options...
Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 This seems to do exactly what you want... or at least I think so! o.o' if(!isset($keyword) || $keyword == '') and so does the code I posted.... Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/#findComment-1336976 Share on other sites More sharing options...
sandy1028 Posted April 13, 2012 Author Share Posted April 13, 2012 Thanks All. How about using this ? if (strlen($keyword) >= 1) { which is better option. Please suggest. Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/#findComment-1336987 Share on other sites More sharing options...
MMDE Posted April 13, 2012 Share Posted April 13, 2012 This seems to do exactly what you want... or at least I think so! o.o' if(!isset($keyword) || $keyword == '') and so does the code I posted.... No, your checked for type as well, and if it's a float zero then it won't equal integer zero. I guess you wrote !== to avoid this problem: <?php $keyword = false; if($keyword == 0){ echo 'false == 0'; } ?> Let me show you what I mean: <?php $keyword = 0.0; if(empty($keyword) && $keyword!==0 ){ echo $keyword.' = empty!'; } ?> Try to run that, and you will see what I mean. It will return: 0 = empty! But you are right, mine is wrong as well. I would have played around with it for a bit more, but I don't have the time at the moment. Thanks All. How about using this ? if (strlen($keyword) >= 1) { which is better option. Please suggest. Sure you can do that, but it won't check if the $keyword is not set, and neither if it's set to true or false. Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/#findComment-1336996 Share on other sites More sharing options...
MMDE Posted April 13, 2012 Share Posted April 13, 2012 Here is a better solution, I at least think this will do what you want: if(!isset($keyword) || $keyword === ''){ } I will probably think about some exception in a while, but I guess until then this is the best I at least can think of. Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/#findComment-1337007 Share on other sites More sharing options...
Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 I used !== because it fit for the value that was requested for the check, the OP said 0 not 0.0 or 0.00000 or 000000.000000000000000 the issue being that PHP is a loose langauge, so 0 vlaues are parsed as false and false is parsed as empty. another way you could work it would be (assuming the normal kwywords are actualy words): <?php $keyword = (string)0.0; if(empty($keyword) && $keyword != '0'){ echo $keyword.' is empty!'; } echo $keyword; ?> or how about <?php $keyword = (string)0.0; if(empty($keyword) && $keyword != chr(48)){ echo $keyword.' is empty!'; } echo $keyword; ?> Quote Link to comment https://forums.phpfreaks.com/topic/260857-value-as-0-treated-as-empty-string/#findComment-1337010 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.