PHP validation bug?

Discussion in 'PHP' started by ignas2526, Mar 1, 2010.

  1. #1
    Hello,
    I have interesting problem when I trying to validate value. The code is valid and works white-out any errors, however result is wrong.
    Code:
    <?php
    $s='12345';
    $i=false;
    if(isset($s)&&($s+=0)>0&&isset($s[4])&&!isset($s[5])){$i=true;}//$i must be true, but its not!
    ?>
    PHP:
    $i must be true, but its false.

    If we separate validation we will get true in all tests:
    <?php
    $i='';
    $s='12345';
    if(isset($s)){$i.=1;}
    $s='12345';
    if(($s+=0)>0){$i.=1;}
    $s='12345';
    if(isset($s[4])){$i.=1;}
    $s='12345';
    if(!isset($s[5])){$i.=1;}
    echo $i;
    ?>
    PHP:
    outputs 1111
    Is this happens only for me?
    Thanks.
     
    ignas2526, Mar 1, 2010 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    if($s && ($s+=0)>0 && strlen($s)==5 && strlen($s)<6){
    $i = 1;
    }
    PHP:
    This seems to be working
     
    Sky AK47, Mar 1, 2010 IP
  3. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yea works for me to, but I need code what I posted to work...
     
    ignas2526, Mar 1, 2010 IP
  4. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #4
    What do you mean? =/
    if($s && ($s+=0)>0 && strlen($s)==5 && strlen($s)<6){
    $i = true;
    }
    PHP:
     
    Sky AK47, Mar 1, 2010 IP
  5. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I mean what this code is 100% valid, but it doesn't work:
    if(isset($s)&&($s+=0)>0&&isset($s[4])&&!isset($s[5])){$i=true;}
    PHP:
     
    ignas2526, Mar 1, 2010 IP