Abdalrahman Shatou
Abdalrahman Shatou

Reputation: 4748

PHP - json_decode() returns null when using echo

I am using the following script to decode a json. Although the var_dump($obj) returns result (similar to one in here Example #1), the echo line doesn't return any result, as if the $obj being null.

<?php

$clientJSONObject = file_get_contents('php://input');
$obj = json_decode($clientJSONObject, TRUE);
var_dump($obj); // working.
echo $obj; // returns nothing.
echo $obj["carrier"]; // returns nothing.

?>

var_dump output:

array(2) {
  ["carrier"]=>
  string(8) "Etisalat"
  ["userLanguage"]=>
  string(2) "ar"
}

Upvotes: 1

Views: 778

Answers (1)

Aravind.HU
Aravind.HU

Reputation: 9472

You cant echo an object property like that , you have to use -> operator

here is the example of a similar thing what you are looking for

echo $obj->{"objectname"}

will print the property name of the json decode object . and I am able to see one more error in your code . you are giving true in caps that is the reason the Jsondecode function is not giving you an array it is still throwing an object

Upvotes: 2

Related Questions