Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: How do get around 0 being treated as a null value?

  1. #1
    Join Date
    Dec 2005
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How do get around 0 being treated as a null value?

    I have a form input in which I would like to submit the the value of 0, but it being treated as a null value. I don't want to have to use 00.

    I'm using this code, what changes do I need to make?
    PHP Code:
    if (!$_POST['description'] | !$_POST['id']) {
    die(
    'You did not complete all of the required fields');


  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP Code:
    if (!$_POST['description'] || !$_POST['id']  && $_POST['id'] != '0') {
    die(
    'You did not complete all of the required fields');

    Treats 0 as a literal string, there may be other ways of doing it but this seems to work.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The string '0' is falsy in PHP: that is to say, '0' == false. Nobody knows quite why; it's counterintuitive and frankly stupid, but hey, it's PHP: we're used to it.

    Anyway, the proper way around it, in this case, is to use the empty() function:

    Code:
    if (empty($_POST['description']) || empty($_POST['id']))
      die('You did not complete all the required fields.');
    Generally, if you need to check two values, use ===, the equality operator, rather than ==, the type-converting equality operator. Use == only if you've carefully considered all the possible types of both sides and explicitly want to allow them all.

    Something else to watch out for is that || (logical OR) is not the same as | (bitwise OR). In many situations, via several type coercions, bitwise operators will act in a similar manner to their logical counterparts, but bitwise operators do not short-circuit, so they will often exhibit worse performance, and additionally in some cases they may exhibit vastly different behaviour: '8' & '4' is falsy, even though '8' && '4' is true.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I can't believe I'm saying this, but for once I think you're wrong

    I've used empty() before and have known it to take "0" as a null string, as it says in the php manual:

    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)
    I also tested the code and as I expected, inputting 0 in a field returned "You did not complete all the required fields".

    I'm pretty sure the way I've done it isn't the most efficient either, but the way you have proposed doesn't seem to work at all. Am I missing something?

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, it seems you're right — that was added in PHP4. I should have read the fine manual before commenting

    The other option here, then, would be:
    Code:
    if (@$_POST['description'] === NULL || @$_POST['id'] === NULL)
    Something peculiar happened when I was trying to test out empty():
    Code:
    php > print empty("");
    php > print empty("0") ? "Yes" : "No";
    php > print (empty("0") ? "Yes" : "No");
    php > print "huh?";
    huh?
    php > print true ? "yes" : "no";
    yes
    php > print empty("0") ? "Yes" : "No";
    php > error_reporting(E_ALL);
    php > print empty("0") ? "Yes" : "No";
    php > echo("Hi");
    Hi
    php > echo(true ? "Yes" : "No");
    Yes
    php > echo(empty(0) ? "Yes" : "No");
    php >
    It seems I can't print the value of an expression containing empty(). Maybe I'm missing something here.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    Quote Originally Posted by Twey View Post
    Ah, it seems you're right — that was added in PHP4. I should have read the fine manual before commenting

    It seems I can't print the value of an expression containing empty(). Maybe I'm missing something here.
    there is also the is_number() if you wish to check whether it is NULL or what, but === is the way to go.

    Re: empty() -- maybe it only works on variables and not expression?

  7. #7
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I don't think $variable === null works either, I just tried it and it didn't stop anything, if I put in a 0 it didn't pick up on it, if I left it blank it did the same thing. There must be a better way to do this, but the null way doesn't seem to work at all.

    Edit:

    PHP Code:
    if ($_POST['description'] === '' || $_POST['id'] === ''
    This seems to work too, allows 0.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Er, yes, of course — I was thinking they'd be undefined, but of course they're only actually likely to be empty strings. That's what you want.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    I don't think $variable === null works either, I just tried it and it didn't stop anything, if I put in a 0 it didn't pick up on it, if I left it blank it did the same thing. There must be a better way to do this, but the null way doesn't seem to work at all.

    Edit:

    PHP Code:
    if ($_POST['description'] === '' || $_POST['id'] === ''
    This seems to work too, allows 0.
    PHP Code:
    if (isset($_POST['id'] && $_POST['id'] === null
    For cases when id is not set (hey, that may happen...what if the user types in the URL directly?)

    Anothing to try may be

    PHP Code:
    $_POST['id'] = intval($_POST['id']);

    // If isset code here 
    Typically, it's better to leave a variable undefined then to have it points to empty for query string, IMHO.

  10. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Twey View Post
    It seems I can't print the value of an expression containing empty(). Maybe I'm missing something here.
    From what I understand, it appears empty() cant take direct input. Even though it should be a mixed var. Hmm... Maybe its being fixed for PHP 6.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •