tags:

views:

93

answers:

5
$extension = "jpg"

    if($extension != "jpg" || $extension != "gif" || $extension != "png") die("only jpg, gif, png acceptable");

it always seems to die().

+6  A: 

You want && not ||.

Read it outloud as:

"If x doesn't equal Y OR x doesn't equal N".

Clearly, it doesn't matter what x equals, as long as Y and N aren't equal, the statement will always be true :)

Noon Silk
Or `if(!($extension == 'jpg' || $extension == 'gif' || $extension == 'png'))`. Yay for De Morgan. 'Course, it's easier to overlook the `!` this way.
Mark
@Mark, De Morgan's theorem is supposed to be a way to *simplify* boolean algebra. You appear to be using it here to make it *more* complex :-)
paxdiablo
yes i was silly with this simple logic. thank you.
ggggggggg
ggg: I wouldn't worry; I'd be embarrassed to say how many times I've made this error in the past; it always helps to read out loud the statement, and even write down a few cases of it, just to check that all is well (and, obviously test :P)
Noon Silk
+5  A: 

Since $extension cannot be "jpg", "gif" and "png" at the same time, at least two of the sub-conditions are true.

And, since you're using or (||) instead of and (&&), any true sub-condition will render the entire condition true.

You want something like:

if (($extension != "jpg") && ($extension != "gif") && ($extension != "png")) {
    die ("only jpg, gif, png acceptable");
}
paxdiablo
+1  A: 
$extension = "jpg"

    if($extension != "jpg" && $extension != "gif" && $extension != "png") die("only jpg, gif, png acceptable");

This may work

Treby
+2  A: 

You are using the NOT EQUAL TO operator with a bunch of ORs.

$extension = "jpg"
if ($extension != "jpg" || 
    $extension != "gif" || 
    $extension != "png") 
    die("only jpg, gif, png acceptable");

You could fix this one of two ways:

  • Switch all || to &&
  • Simplify it be having an array of valid extensions:

$valid = array('jpg' => true, 'gif' => true, 'png' => true);
if (!isset($valid[$extension])) {
    // not a valid extension
}
cballou
+1 professional coding
Treby
Why not just use $valid = array('jpg','gif','png'); if (!in_array($extension, $valid) { ... } ?
Darren Newton
@Darren - using `in_array()` requires iteration over the entire array whereas setting the array keys allows you to simply check if the key is set. It's a simple optimization.
cballou
In the case you might want to check that the value is equal to true. That allows you to turn some extensions on and some off. And if it is unknown, it is off by default.
Chacha102
The in_array() solution is indeed slightly less efficient. However, it's easier to read. Assuming this was fairly infrequently executed code, I'd probably opt for the in_array() solution. No sense in sacrificing readability to save a few nanoseconds.
Frank Farmer
@Frank - I never opt for slower code if I can write something faster in the same amount of time. Also I'd argue that readability is not reduced as `!isset($valid[$extension]` jumps out at you in plain English.
cballou
A: 

Reasons are already explained, moreover you can simply do this, with the result you expected:

$extension = "jpg";
if(!in_array($extension,array("jpg","gif","png"))){
    die("only jpg, gif, png acceptable");
}
jerjer