Home » Php » php – if statement returning true

php – if statement returning true

Posted by: admin July 12, 2020 Leave a comment

Questions:

Can someone tell me why, when selecting a psd file, the if statement in the php code passes as true and echos “image/vnd.adobe.photoshop”?

<?php

if (isset($_POST['submit'])) {
    foreach ($_FILES["myimages"]["error"] as $key => $error) {
        $tmp_name = $_FILES["myimages"]["tmp_name"][$key];
        $name = $_FILES["myimages"]["name"][$key];
        $imagetype = $_FILES['myimages']['type'][$key];

        if ($imagetype == "image/jpeg" || "image/gif") {
            echo $imagetype;
        }
    }
}

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<form method="post" enctype="multipart/form-data" action="<? echo basename(__file__); ?>">
    <input type="file" name="myimages[]" multiple>
    <input name="submit" type="submit" value="submit">
</form>

</body>
</html>
How to&Answers:

Because this is wrong

if( $imagetype == "image/jpeg" || "image/gif" ) { /*...*/ }

Should be

if( $imagetype == "image/jpeg" || $imagetype == "image/gif" ) { /*...*/ }

Or even

if( in_array($imagetype, ["image/jpeg", "image/gif"]) ) { /*...*/ }

That is, because non-empty string is considered true, so the IF condition was met.

Answer:

This is because bitwise operator has lower priority than equality check. It goes like this:

  1. You have

    if ($imagetype == "image/jpeg" || "image/gif") {
    
  2. Equality resolves to boolean first, and your expression becomes:

    if (false || "image/gif") {
    
  3. Non-empty strings are treated like true when used in expression. That means we have

    if (false || true) {
    
  4. And the result of this, of course, is true, so the if-block executes.

Answer:

Your || statement is incorrect

$imagetype == "image/jpeg" || 
$imagetype == "image/gif"

What’s happening is “image/gif” is returning true and that true is being or’d with the false returned by
$imagetype == “image/jpeg”. false || true == true

Answer:

You can also have the following condition :

if ( $imagetype == ("image/jpeg" || "image/gif") ) {
    echo $imagetype;
}

The other answers are good, this is just another way to solve it 🙂

Answer:

The if statement as you wrote it would be expected to evaluate as TRUE.

This is because you’re not comparing “image/gif” to anything, you’re simply evaluating it as a boolean value. PHP converts string values (that are not "0", or empty ("")) to TRUE when they’re evaluated as a boolean. So, essentially, your IF statement is saying “if $imagetype is equal to "image/jpeg" OR "image/gif" is TRUE, then do this stuff.

Your if condition will always pass, because “image/gif” will ALWAYS be evaluated as true, so it doesn’t matter what $imagetypes value actually is.

Answer:

if($imagetype == "image/jpeg" || "image/gif")

is equivqlent to:

$condition1 = $imagetype == "image/jpeg";
if($condition1 || "image/gif")

which will be evaluated in this case as :

if($condition1 || true)

which evaluates to true no matter what is the value of $condition1

because if(false || true) returns true

Another point: I suggest to use === operator instead of ==, so your final code should be:

if($imagetype === "image/jpeg" || $imagetype === "image/gif")