if语句返回true

dav*_*e85 7 php

有人可以告诉我为什么,当选择一个psd文件时,php代码中的if语句传递为true和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>
Run Code Online (Sandbox Code Playgroud)

Mig*_*ork 9

因为这是错误的

if( $imagetype == "image/jpeg" || "image/gif" ) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

应该

if( $imagetype == "image/jpeg" || $imagetype == "image/gif" ) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

甚至

if( in_array($imagetype, ["image/jpeg", "image/gif"]) ) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

也就是说,因为非空字符串被认为是真的,所以满足了IF条件.

  • 还请解释_why_它会返回意外的真实. (3认同)
  • 说明:`if(anyCondition ||"image/gif")`总是返回true,因为`image/gif`是一个非空字符串.我猜OP认为`||`做了别的事情. (2认同)