SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Simple if structure always returns true when measuring strings

    OK, this one is just plain weird.

    I have coded php for years and have never seen this one before, yet it is very simple.

    I am working on a component for Joomla, but that shouldn't really matter.

    I am getting a variable from the request string with this Joomla function:

    $var = mosGetParam( $_REQUEST, 'var', '0' );

    All it does is set the var to 0 if it does not exist. I can see in my query string that the $var is set to 0 and when I echo the $var it also returns 0.

    Now... here comes the scary part.

    When I run an if structure on the $var it always returns true if what I compare it to is a string. Like this

    Code PHP:
    if ($var == "somestring") {
      echo "var is somestring: ".$var;
    }

    But the echo is var is somestring: 0

    If i do the following:

    Code PHP:
    if ($var == "1") {
      echo "var is 1: ".$var;
    }

    It fails and does not validate, even though 1 is also a string in this comparison.

    What is going on?

    I have tried === but that is only for boolean - right? And I still want to know what is going on.

    P.S. What I am really trying to do is check a check box if a certain value is "yes" but when the value id 0 and I check for "yes" it still returns true?!?!
    Soren Beck Jensen

    http://www.automaticbacklinks.com - Free link exchange network
    http://www.notwebdesign.com - Joomla web design in Spain

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Whistler, Canada
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CT Internet View Post
    I have tried === but that is only for boolean - right?
    same as == but this one only returns true if they're also the same type (array, object, bool, string etc.). But I don't know where it goes wrong to be honest...

    Cheers,
    Pepe

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CT Internet
    PHP Code:
    if ($var == "somestring") {
      echo 
    "var is somestring: ".$var;

    But the echo is var is somestring: 0
    That can't be true. You must be doing something else. Doublecheck that this is the exact same code, as the code which is giving that result.

  4. #4
    SitePoint Addict
    Join Date
    Sep 2005
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think in PHP...

    if (0 == 0) // this is true
    if (0 == "something") // this is true because "something" evaluates to a numeric 0

    You can try this:
    if ("xxx".$var == "xxxsomething")

    that makes sure you convert $var to a string.
    There's probably a better way like using ===
    PHP Shopping Cart Software Easy Ecommerce Shopping Cart Script.
    PHP Super Cart is 100% template driven.


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
  •