|  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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
38 - 8 = ?
Subscribe to this entry?

 [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-2023 The PHP Group
All rights reserved.
Last updated: Wed Sep 27 14:01:25 2023 UTC