Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement returning true

Tags:

php

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>
like image 509
davelowe85 Avatar asked Aug 02 '13 14:08

davelowe85


People also ask

Does an if statement return true or false?

Notice that the boolean in the if-test (true or false) happens to be the same as the value we want to return. If the test value is true, we return true. If the test value is false, we return false.

Why is my IF statement always returning false?

An if statement does not evaluate whether the command inside its condition ran successfully. It will only check the value (in your case the return of your command) and cast it to a bool . Copy-Item does not return anything by default, and that's why your if statement is always false , because [bool]$null is $false .

What is return TRUE means?

returning true or false indicates that whether execution should continue or stop right there.

Why does true == true return false?

Because they don't represent equally convertible types/values. The conversion used by == is much more complex than a simple toBoolean conversion used by if ('true') . So given this code true == 'true' , it finds this: "If Type(x) is Boolean , return the result of the comparison ToNumber(x) == y ."


3 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.

like image 105
MightyPork Avatar answered Oct 14 '22 02:10

MightyPork


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.

like image 31
klkvsk Avatar answered Oct 14 '22 03:10

klkvsk


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

like image 2
dseibert Avatar answered Oct 14 '22 03:10

dseibert