Вопрос

Here is my code:

if( ( ! isset($_GET['year']) ) || ( ! isset($_GET['make']) ) || $_GET['make'] == 0 ) {
    echo 0;
    exit;
}

It keeps returning 0... when year and make are set and make does not equal 0...

This is the URL I'm passing through:

http://mopar.localhost/ajax/populateVehicle.php?column=model&year=2014&make=Chrysler

Why does this keep returning 0? Even if I delete the $_GET['make'] == 0, it still comes back as 0.. can you not have two isset() conditions?

Это было полезно?

Решение

It looks like PHP is treating a string compared to an integer as true ($_GET["make"] == 0).

The reason for this is $_GET["make"] is being implicitly cast as an int for the comparison. When you try to convert a string to an int, on failure the value 0 is returned.

Compare $_GET['make'] to the string "0" instead.

if ( 
        ( ! isset( $_GET['year'] ) ) || 
        ( ! isset( $_GET['make'] ) ) || 
        $_GET['make'] == "0"
    ) {
    echo 0;
    exit;
}

Другие советы

@Andrew, $_GET['make'] === 0 will be never identical, all values from _POST, _GET was treated as string, you've to do the cast if you want to change its type.

P.S: i can't comment because doesn't have reputation for it.
Sorry.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top