Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: problem in comparing values......

  1. #1
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Unhappy problem in comparing values......

    hi friends... i need to compare two values , but i fails.....

    value 1 : it is coming from database directly... the value is ( 0 , 1 or 4)any1
    value 2 : it is the return value from a function ... the value is (0 or 1 only)

    i want to compare if (value1 <= value2)

    but it does not compares.. cz it takes value1 as string ...

    so in cast the type into int.. but still it have same problem....

    it works if i write the condiion : if(value1 == value2)
    it works... but i need comparison of less than...

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Try forcing it to be a number. Here's an easy trick:

    if ($v1+1 <= $v2+1) { ....

    That's not very pretty, but it will do what you need.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by gurmeet View Post
    hi friends... i need to compare two values , but i fails.....

    value 1 : it is coming from database directly... the value is ( 0 , 1 or 4)any1
    value 2 : it is the return value from a function ... the value is (0 or 1 only)

    i want to compare if (value1 <= value2)

    but it does not compares.. cz it takes value1 as string ...

    so in cast the type into int.. but still it have same problem....

    it works if i write the condiion : if(value1 == value2)
    it works... but i need comparison of less than...
    to be clear, you're saying that this doesn't work? it should:
    PHP Code:
    <?php

    if( (int)$value1 <= (int)$value2 ){ /* do stuff */ }

    ?>

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    And that's probably a better way than what I suggested. The only advantage to mine is that you don't need to remember that syntax, just do an operation requiring numbers and go from there. But since traq's method is the correct way to set this, I'd suggest using it instead.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    If type casting isn't working for some reason, you could also do a variation on djr's method:
    PHP Code:
    <?php

    // get your value from the function or DB call
    $value value_from_function();
    // adding zero to the value doesn't change it, but should cast it as an integer
    $value += 0;
    // then use it normally

    ?>
    Of course, type casting should work just fine. Did I understand your first post correctly that it is not?

  6. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Is (int) a short hand way of settype?
    Corrections to my coding/thoughts welcome.

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    hmm... I'm pretty sure it's a basic language construct, not "shorthand" for the function settype(). (I had actually never seen that function before.) It sure is less work, though.

    Now this is crazy:
    Quote Originally Posted by php.net
    PHP Code:
    <?php

    $foo 
    10;            // $foo is an integer
    $str "$foo";        // $str is a string

    ?>
    Kinda like the cheat djr suggested above.
    Last edited by traq; 07-15-2010 at 03:18 AM.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The other easy way to force a string is: $foo = $foo.'';
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Quote Originally Posted by djr33 View Post
    The other easy way to force a string is: $foo = $foo.'';
    Note that those are two single quotes. (I was about to post something saying you needed to close those double quotes )
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Double quotes would be fine. I've gotten into a habit of only (with rare specific exceptions) using single quotes, not double, in my PHP. But double does the same thing here.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •