PHP comparison is not returning correct result

58 views Asked by At

I'm looking up a value in a SQL table. The table is returning the value "text", but when I run a comparison on it I get false. Is there a reason this would return false?

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){

    $valType = $row['valType']; //Result: "text"

    if("text" === $valType) {
        $result = "true";
    } else {
        $result = "false";
    }
    echo $result; //Result: "false"
}

Thanks to the help of @Rocket Hazmat, I found my issue by using var_dump($row['valType']). It was a case sensitivity issue.

1

There are 1 answers

2
makallio85 On

In your code example $valType is not "text", it is something else. See below example.

<?php
    $valType = "text";
    if("text" === $valType) {
        $result = "true";
    } else {
        $result = "false";
    }
    echo $result; //Prints "true"
?>

This could be shortened like this:

<?php
    $valType = "text";
    echo ("text" === $valType ? "true" : "false"); //Prints "true"
?>