0

I have the following code that returns a bunch of results back from an API, everything inside "" works but the Boolean values that dont have "" dont return, is there something im missing from code for this??

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "URL",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        "authorization: KEY"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

$json_a2 = json_decode($response, true);

What is been returned back from the API is

"VehicleRegistration": {
        "DateOfLastUpdate": "2014-12-19T00:00:00",
        "Colour": "BLACK",
        "AbiBrokerNetCode": null,
        "EngineCapacity": "1598",
        "TransmissionCode": "M",
        "DtpMakeCode": "DF",
        "Exported": false,
        "YearOfManufacture": "2014",
        "WheelPlan": null,
        "DateExported": null,
        "Scrapped": false,
}

The false is not showing when i return the results.

4
  • The trailing comma after false makes this invalid JSON. Can you post EXACTLY what you're getting back from the API? Commented Jan 3, 2017 at 16:26
  • 1
    false and true without quotes is perfectly valid. Commented Jan 3, 2017 at 16:39
  • 3
    My crystal balls suggests you've decoded JSON and tried to echo boolean values, which of course cast as string as '1' and '' (empty string) because that's how PHP is. Commented Jan 3, 2017 at 16:39
  • 1
    Use var_dump($json_a2) to see everything that's returned. Commented Jan 3, 2017 at 16:56

1 Answer 1

0

The false will not show as php will always display a boolean false value as empty string.

In PHP false, null, empty string, and 0 will all equal false.

If you need to check to see if your var is really false and not any other then use:

$my_var === false;

or

false === $my_var;

Note triple equals

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.