...

View Full Version : If statement with ||OR - Need help



absoleet
11-16-2009, 01:26 AM
here's what I'm working with



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


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

tomws
11-16-2009, 02:31 AM
Even though the value of $extension is .jpg,

Logic error. Consider your statement above and read through our conditions again:

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:

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.

oesxyl
11-16-2009, 03:45 AM
if( $extension !~ /\.(jpg|jpeg|zip|rar)$/){
print "<br>$extension is not a valid file type. Must be: .zip, .rar, .jpg<br>";
}


best regards

absoleet
11-16-2009, 07:59 PM
Thank you both, very much appreciated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum