|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81532 Change of $depth behaviour in json_encode() on PHP 8.1
Submitted: 2021-10-17 12:43 UTC Modified: -
From: benjamin dot morel at gmail dot com Assigned:
Status: Closed Package: JSON related
PHP Version: 8.1.0RC4 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: benjamin dot morel at gmail dot com
New email:
PHP Version: OS:


 [2021-10-17 12:43 UTC] benjamin dot morel at gmail dot com
The behaviour of the $depth parameter of json_encode() has changed when encoding objects.

On PHP 8.1, all three examples below successfully encode the input object, while
on PHP <= 8.0, json_encode() returns false as this exceeds the max depth.


If we replace the $a object with an empty array [], it works:

Test script:
// depth 1
$a = new \stdClass();

// depth 2
$b = new \stdClass();
$b->x = $a;

// depth 3
$c = new \stdClass();
$c->x = [$a];

var_export(json_encode($a, 0, 0)); echo "\n";
var_export(json_encode($b, 0, 1)); echo "\n";
var_export(json_encode($c, 0, 2));

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-18 06:48 UTC] benjamin dot morel at gmail dot com
Sorry, I mixed the expected and actual.
The expected result is false.
 [2021-10-18 10:42 UTC]
Automatic comment on behalf of nikic
Log: Fixed bug #81532
 [2021-10-18 10:42 UTC]
-Status: Open +Status: Closed
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Dec 04 21:03:34 2021 UTC