# Change of $depth behaviour in json_encode() on PHP 8.1

- Thread: https://externals.io/message/116279
- Original archive: https://news-web.php.net/php.internals/116279
- Started by Benjamin Morel on 2021-10-17T10:05:34+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #116279 — Benjamin Morel — 2021-10-17T10:05:34+00:00

URL: https://externals.io/message/116279#116279

Hi internals,

I noticed a change of behaviour on PHP 8.1:

https://3v4l.org/DoG4A

```
// 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));
```

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

I couldn't find a note about this in the migration guide for PHP 8.1. Is
this documented, or is this a regression that should be fixed?

Thanks in advance,
— Benjamin

---

## #116280 — Kamil Tekiela — 2021-10-17T11:39:38+00:00

URL: https://externals.io/message/116279#116280 · In reply to: #116279

Hi Benjamin,

This is a bug. Please report it on https://bugs.php.net/

See https://3v4l.org/vFgh0
https://github.com/php/php-src/commit/f9f8c1c79cac1b03279190e0c5513a51881615f9
https://github.com/php/php-src/pull/6811

Best Regards,
Kamil

---

## #116281 — Benjamin Morel — 2021-10-17T12:45:11+00:00

URL: https://externals.io/message/116279#116281 · In reply to: #116280

>
>
> This is a bug. Please report it on https://bugs.php.net/
>
> See https://3v4l.org/vFgh0
>
> https://github.com/php/php-src/commit/f9f8c1c79cac1b03279190e0c5513a51881615f9
> https://github.com/php/php-src/pull/6811
>
> Best Regards,
> Kamil
>

Thank you! Reported as https://bugs.php.net/bug.php?id=81532
