SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Strange failure of Php - makes NO sense!

    Greetings,

    Why in Gods name is this condition firing?
    That is I have this Php code:

    if (!isset($_POST['country']) OR ($_POST['country'] == 0)) {
    $error = 'You have not indicated what Country you are from.<p>Country: ' . $_POST['country'];
    }


    But $_POST['country'] is set! That is when I do:
    echo $error, it produces:

    You have not indicated what Country you are from.
    Country: Canada


    Thank you for this insight, since I am totally stupmed by WHY in GODs name this condition is firing where as you can see $_POST['country'] is set!

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    It's the == 0 condition that's firing. the integer value of a string that does not start with digits is 0.

  3. #3
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    What StarLion said. You want to use null.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  4. #4
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I dont get it!
    How is it that a String is equaling ZERO (0)???

    FYI, I changed the test to be:

    if (!isset($_POST['country']) OR ($_POST['country'] == '')) {
    $error = 'You have not indicated what Country you are from.<p>Country: ' . $_POST['country'];
    }

    and it works Ok now.
    But I do not get it how in GODs name a string, for example 'Canada', is equaling 0?
    Or did the basic of Computer Science completely change and I missed that


    Quote Originally Posted by StarLion View Post
    It's the == 0 condition that's firing. the integer value of a string that does not start with digits is 0.

    Anoox search engine volunteer

    www.anoox.com

  5. #5
    SitePoint Member
    Join Date
    May 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP tries to convert your string into a number, so when comparing strings it's just to check if the string is empty or equal to null. I also find it weird how PHP equates values of converting to bool for that matter.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    It's not that difficult to wrap your head around?

    String: "12534Somethinng"
    INT value 12534.

    String "Something"
    INT value 0.

    String "Some9342342"
    INT value 0.

    You could also have Type-specific comparisoned (===), which would return false because String != int.

    http://www.php.net/manual/en/languag...ing.conversion

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ...because when you compare "Canada" to an integer, PHP casts "Canada" into an Integer for comparison. When it does this, it converts it into a 0.

    PHP Code:

    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    var_dump(
      (int)
    'Canada'
    );

    # int(0)
    See Type-Juggling in the manual.
    Last edited by AnthonySterling; Sep 16, 2011 at 09:47. Reason: Goddam crappy editor!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well personally I think this is a design flaw in Php.
    I mean why in Gods name should string 'Canada' equal 0!
    But it is the way it is.
    So thanks your intel anyway on this matter.

    Quote Originally Posted by StarLion View Post
    It's not that difficult to wrap your head around?
    String: "12534Somethinng"
    INT value 12534.

    String "Something"
    INT value 0.

    String "Some9342342"
    INT value 0.

    You could also have Type-specific comparisoned (===), which would return false because String != int.

    http://www.php.net/manual/en/languag...ing.conversion

    Anoox search engine volunteer

    www.anoox.com

  9. #9
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Well personally I think this is a design flaw in Php.
    Its called being a dynamic programming language. If you cannot be bothered to understand that, and call it a flaw then I suggest you forget PHP and move on to a strict typed language.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try empty instead of isset

    PHP Code:
    if (empty($_POST['country'])) {
            
    $error 'You have not indicated what Country you are from.<p>Country: ' $_POST['country'];

    From manual:
    The following things are considered to be empty:

    "" (an empty string)
    0 (0 as an integer)
    0.0 (0 as a float)
    "0" (0 as a string)
    NULL
    FALSE
    array() (an empty array)
    var $var; (a variable declared, but without a value in a class)

  11. #11
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well I could have thought of a nicer way of saying the same thing
    Good day anyway

    Quote Originally Posted by logic_earth View Post
    Its called being a dynamic programming language. If you cannot be bothered to understand that, and call it a flaw then I suggest you forget PHP and move on to a strict typed language.

    Anoox search engine volunteer

    www.anoox.com

  12. #12
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by WorldNews View Post
    I mean why in Gods name should string 'Canada' equal 0!
    What should it equal?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •