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
    New Coder
    Join Date
    Nov 2009
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    If statement with ||OR - Need help

    here's what I'm working with


    Code:
     if ( $extension ne ".jpg" || ".jpeg" || ".zip" || ".rar" ) 
    {
    print "<br>$extension is not a valid file type. Must be: .zip, .rar, .jpg<br>";
    }
    Code:
    if ( $extension ne ".jpg" || $extension ne ".jpeg" || $extension ne ".zip" || $extension ne ".rar" ) 
    {
    print "<br>$extension is not a valid file type. Must be: .zip, .rar, .jpg<br>";
    }

    Even though the value of $extension is .jpg, the statement is still printing the error message, in both examples. What am I missing? I'd like to have it only tripped if the extension is NOT jpg, jpeg, zip or rar.

    45 mins of google / trial and error have yet to get me anywhere.

    - Thanks

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by absoleet View Post
    Even though the value of $extension is .jpg,
    Logic error. Consider your statement above and read through our conditions again:
    Code:
    if ( $extension ne ".jpg" || $extension ne ".jpeg" || $extension ne ".zip" || $extension ne ".rar" )
    So, $extension is jpg, you say. Then that statement evaluates like this:

    $extension not equal '.jpg'? False.
    $extension not equal '.jpeg'? True.
    $extension not equal '.zip'? True.
    $extension not equal '.jpg'? True.
    "False or true or true or true" evaluates to TRUE. TRUE found; enter block.

    It's doing just what you've coded it to do. What you've said you want it to do is different. You want it to enter that block if the extension is none of those. That might look like this:
    Code:
    if ( $extension ne ".jpg" && $extension ne ".jpeg" && $extension ne ".zip" && $extension ne ".rar" )
    Try out the logic in plain language as I did above...

    $extension not equal '.jpg'? False.
    $extension not equal '.jpeg'? True.
    $extension not equal '.zip'? True.
    $extension not equal '.jpg'? True.
    "False and true and true and true" evaluates to FALSE. FALSE found; do not enter block.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    absoleet (11-16-2009)

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
     if( $extension !~ /\.(jpg|jpeg|zip|rar)$/){
      print "<br>$extension is not a valid file type. Must be: .zip, .rar, .jpg<br>";
    }
    best regards

  • Users who have thanked oesxyl for this post:

    absoleet (11-16-2009)

  • #4
    New Coder
    Join Date
    Nov 2009
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you both, very much appreciated.


  •  

    Posting Permissions

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