Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

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

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

    Default

    That would make some sense, but it seems it should trigger an error or at least a warning, rather than just ignoring the whole statement.
    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!

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

    Default

    Quote Originally Posted by Twey View Post
    That would make some sense, but it seems it should trigger an error or at least a warning, rather than just ignoring the whole statement.
    By default, PHP is rather lenient. If it comes across a variable not declared before and is being used, it will create it on the spot. To catch those errors, put this at the start of any PHP script

    PHP Code:
    error_reporting(E_ALL); 
    In fact, this is my SOP. Helps to catch a lot of bugs due to mis-spelling and such.

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

    Default

    I was not using any variables, and I had turned on E_ALL error-reporting (as you can see from the transcript of the session I posted). I agree that it is usually a good idea to turn on E_ALL in new scripts.
    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. #14
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    You have to feed empty() a variable. Constants cause parse errors. I don't know why you weren't getting errors Twey.

    PHP Code:
    php > echo empty(0) ? 'yes' 'no';

    Parse errorsyntax errorunexpected T_LNUMBERexpecting T_STRING or T_VARIABLE or '$' in php shell code on line 1
    php 
    > print empty(0) ? 'yes' 'no';

    Parse errorsyntax errorunexpected T_LNUMBERexpecting T_STRING or T_VARIABLE or '$' in php shell code on line 1
    php 
    $var 0;

    php > print empty($var) ? 'yes' 'no';
    yes
    php 

    My recommendation for the OP is:

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


  5. The Following User Says Thank You to JasonDFR For This Useful Post:

    toe_head2001 (04-07-2009)

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

    Default

    is_numeric() is good, but I stand by my recommendation of === for the former.
    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!

  7. #16
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    $_POST['description'] = '';
    $_POST['id'] = 100;

    if ( 
    $_POST['description'] === NULL || !is_numeric($_POST['id']) ) {
        die(
    'You did not complete all of the required fields');

    The problem with === NULL is that it doesn't test for an empty string. If the POST variable is set, which if the form was submitted, it will be, NULL won't filter anything.

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

    Default

    I think he was in favour of:

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

    Not:

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


  9. #18
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    I think he was in favour of:

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

    Not:

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

    Oh. Ok.

    What is the difference between =='' and ===''. Is 0 =='' true, whereas 0 ==='' not true? In the case of posted variables which I think are always going to be strings, why does === make a difference?

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

    Default

    Well, for example, '0' == false. Strings can be coerced too.
    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!

  11. #20
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I'm sorry Twey, please bare with me.

    I am not seeing a difference:

    PHP Code:
    <?php

    $_POST
    ['description'] = '0';
    $_POST['id'] = 100;

    // BETWEEN

    if ( $_POST['description'] === '' || !is_numeric($_POST['id']) ) {
        die(
    'You did not complete all of the required fields');
    }

    // AND

    if ( $_POST['description'] == '' || !is_numeric($_POST['id']) ) {
        die(
    'You did not complete all of the required fields');
    }

    exit;
    What am I missing? Or is it that you are saying since we know the $_POST variable is a string we should explicitly test for the value and type, hence === '' , where '' is an empty string ?

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
  •