Jump to content

value as 0 treated as empty string


sandy1028

Recommended Posts

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";
        }

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.... :shrug:

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.