php "if" unable to determine?

Discussion in 'PHP' started by Adulu, Jun 17, 2009.

  1. #1
    <?php
    $x=27;
    $rs=0.2;
    
    for ($i=1;$i<=15;$i++)
    {
    $x-=$rs;
    
    echo "$x<br>";
    
    if($x===25.2)
    {
    echo "ok";
    }
    
    }
    ?>
    PHP:
    if($x===25.2)
    {
    echo "ok";
    }


    unable to determine 25.2
    what's error with it?
     
    Adulu, Jun 17, 2009 IP
  2. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try
    if((string)$x == "25.2")
    {
    echo "ok";
    }
    PHP:
     
    Vbot, Jun 17, 2009 IP
  3. James Barcellano

    James Barcellano Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    You are using the === operator, so you have to be SURE you are passing in a float as $x.
     
    James Barcellano, Jun 17, 2009 IP
  4. pubdomainshost.com

    pubdomainshost.com Peon

    Messages:
    1,277
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    just change triple = to double ( compare value ==) and it would be alright.
     
    pubdomainshost.com, Jun 17, 2009 IP
  5. SunstarShop

    SunstarShop Peon

    Messages:
    582
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I am confused,for i am learning the basic php, just start with the phpinfo(); glad to see you discuss the php, i am gald to learn it here!
     
    SunstarShop, Jun 17, 2009 IP
  6. Adulu

    Adulu Peon

    Messages:
    2,791
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It's work by your solution.
    thanks so much.
     
    Adulu, Jun 17, 2009 IP
  7. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #7
    It's Possible buddy:
    I downloaded a code snippet from the internet, a long time ago. The code contained === in a if() decision, I checked the code thorowly, because it was giving error on execution. I found that the coder (who posted the code on some site), added an extra =, may be it was a typo, or he was not intended to reveal his fully-working code.
     
    techbongo, Jun 17, 2009 IP
  8. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You use === and == for different reasons.

    $t === "1" would mean that the variable $t was stored as "1" -- an exact match
    $t == "1" would mean that whatever is stored in $t is equivalent to 1.
    $t = 2-1; will return a true with ==, but a false with ===.
     
    SHOwnsYou, Jun 17, 2009 IP
  9. ven

    ven Member

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9
    Better explanation:
    php.net/manual/en/language.operators.comparison.php
     
    ven, Jun 17, 2009 IP