Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Wierd variable problem.

    $lightning_def0 = $gen['lightning_def0'];

    ^^ This is a variable i get from my database

    if($lightning_def0 == "ALL") {
    $p1_w1_def_lightningicons = "ALL";
    }else {
    $p1_w1_def_lightningicons = rand($lightning_def0 , $lightning_def1);
    }

    This works fine when $lightning_def0 is a number from 1-10. But when i enter 0 it automatically assumes 0 is ALL. Which is wierd =/.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Please have a look at the recent thread
    2 variables with the same string doesn't match.
    You can't compare strings with "==", instead use strcmp()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You can compare strings with "==", however if they aren't the same it will return false. If there's any chance the string won't be the same case use strtoupper() or strtolower() before comparing them.

    Edit: If you need to compare them based on their type use "==="
    Last edited by Inigoesdr; 08-31-2007 at 03:14 AM.

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    When you compare a number with a string like:
    PHP Code:
    $x 0;
    $y 'Hello World';

    $xIsY = ($x == $y); 
    the string will be converted to a number using the usual conversion rules.

    That means in your case "ALL" will be converted to 0 (zero), which of course is equal to 0.

    If you don't want this to happen cast $lightning_def0 to a string before comparing.

    dumpfi
    Last edited by dumpfi; 09-02-2007 at 03:52 PM.
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

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