php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60394 Return value for json_encode() on failure not listed
Submitted: 2011-11-26 20:25 UTC Modified: 2011-11-27 09:02 UTC
From: papersnowman at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: papersnowman at gmail dot com
New email:
PHP Version: OS:

 

 [2011-11-26 20:25 UTC] papersnowman at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.json-encode#refsect1-function.json-
encode-returnvalues
---
Documentation shows expected return value on success, but not failure. I would 
presume === false, but it is nice to be able to confirm.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-27 03:02 UTC] frozenfire@php.net
-Status: Open +Status: Bogus
 [2011-11-27 03:02 UTC] frozenfire@php.net
This function is, surprisingly, properly documented. I was working on resolving 
this bug, when I checked the sources for the function, and found that there is 
no non-error situation where the function will return anything but an encoded 
string.

The reason for this is all types can be encoded, including null. There are no 
values which you could pass to json_encode that would not result in a string 
being returned. The only way for it to fail is for you to not pass it an 
incorrect number of parameters, or some other error in syntax.
 [2011-11-27 08:02 UTC] irker@php.net
Not all types can encode. For example: Resource.

php -r "var_dump(json_encode(fopen('/tmp/temp', 'w+')));"
PHP Warning:  json_encode(): type is unsupported, encoded as null in Command line 
code on line 1
string(4) "null"

But, I agree that it anyway returns a string ("null" string)
 [2011-11-27 09:02 UTC] papersnowman at gmail dot com
That being the case, would it be worth modifying the "Return Values" section in the documentation from "Returns a JSON encoded string on success.", since there seems to be no failure case as "on success" seems to imply? something like "Returns a JSON encoded string in all cases" or possibly something mentioning the null return case?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 07:01:27 2024 UTC