添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In the PHP documentation for json_decode it says it can return TRUE , FALSE , NULL .

Could some help me understand when it would return FALSE ? I understand invalid JSON will return NULL , but when would the other two be returned if not the actual JSON value?

Thanks

From what I can see, it would only return TRUE or FALSE if that was the actual value decoded. Ignacio Vazquez-Abrams Oct 28, 2013 at 0:27 "if not the actual JSON value" Sorry but what makes you believe true or false could be returned in any other case? RandomSeed Oct 28, 2013 at 0:41

A value can be a string in double quotes, or a number, or true or false or null , or an object or an array.

Both objects and arrays have special syntax in JSON representation (wrapped in {} and [] respectively), so they can't be mixed up with false in any case. The same goes with string - it's wrapped in "" (double quotation marks). As for Numbers, they have to contain at least one digit - so cannot be confused with false (and true and null ) too.

So that leaves us with the only case: when json_encode processes an object having redefined its JSON representation. For example (PHP 5.4+):

class FalsyFoo implements JsonSerializable {
  public $foo;
  public function __construct($f) {
    $this->foo = $f;
  public function jsonSerialize() {  
    return false; 
$f = new FalsyFoo(true);
$fj = json_encode($f);
var_dump( $fj ); // string(5) 'false'
var_dump( json_decode($fj) ); // bool(false)

Technically, we still work with false value here, but the source is obviously different.

If you're still not convinced, check the source code of json_decode, which calls php_json_decode_ex after checking the arguments. This, in turn, calls parse_JSON_ex first, which operates over the predefined state transition table; the latter has only one set of states leading to false value as result. If this call fails somehow, value is checked directly:

if (str_len == 4) {
  if (!strcasecmp(str, "null")) {
    /* We need to explicitly clear the error 
         because its an actual NULL and not an error */
    jp->error_code = PHP_JSON_ERROR_NONE;
    RETVAL_NULL();
  } else if (!strcasecmp(str, "true")) {
    RETVAL_BOOL(1);
} else if (str_len == 5 && !strcasecmp(str, "false")) {
  RETVAL_BOOL(0);

... and that's the only case when return_value is set to boolean.

haha... but is this the only way? I'm wondering when it would not return something other than NULL or the json value itself – d-_-b Oct 27, 2013 at 23:53 In other words, you're asking how can false value be encoded in any other way than as a string 'false' (case-insensitive)? – raina77ow Oct 27, 2013 at 23:59 Updated the answer. Sorry, I always thought it's kind of obvious that false value cannot be represented as anything else but false literal. ) Remember, expressions are not encoded: their results are. – raina77ow Oct 28, 2013 at 0:13

The documentation says that values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. This means that if the booleans true orfalse are in the object to be encoded, they will be shows as TRUE or FALSE, and the same for null. For example:

json_decode('["hello",true]');

would return:

["hello",TRUE]

It doesn't mean that json_decode will return values of true, false, or null

i did read the question, and you said nothing about incorrectly interpreting the documentation and you didn't comment about it either. So, in other words, you downvoted my answer for no reason – markasoftware Oct 28, 2013 at 1:46

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.