3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Thing { public function __construct( public readonly string $name, public readonly ?Thing $left = null, public readonly ?Thing $right = null, ) { } public function hasThings(): bool { return $this->left || $this->right; } public function getThingDepth(): int{ if(!$this->hasThings()) { return 1; } return max($this->left->getThingDepth(), $this->right->getThingDepth()) + 1; } public function getHtml(): string { $buf = []; $buf[] = '<table>'; $buf[] = '<tr>'; $buf[] = '<td>'; $buf[] = $this->name; $buf[] = '</td>'; if($this->hasThings()){ $buf[] = '<td>'; $buf[] = $this?->left->getHtml() ?? '-'; $buf[] = $this?->right->getHtml() ?? '-'; $buf[] = '</td>'; } $buf[] = '</tr>'; $buf[] = '</table>'; return implode("\n", $buf); } } $me = new Thing( 'Child', new Thing('Dad', new Thing('Dad\'s dad'), new Thing('Dad\'s mom')), new Thing('Mom', new Thing('Mom\'s dad'), new Thing('Mom\'s mom')), ); echo $me->getHtml();
Output for 8.1.30 - 8.1.33, 8.2.3 - 8.2.29, 8.3.5 - 8.3.27, 8.4.1 - 8.4.16, 8.5.0 - 8.5.1
<table> <tr> <td> Child </td> <td> <table> <tr> <td> Dad </td> <td> <table> <tr> <td> Dad's dad </td> </tr> </table> <table> <tr> <td> Dad's mom </td> </tr> </table> </td> </tr> </table> <table> <tr> <td> Mom </td> <td> <table> <tr> <td> Mom's dad </td> </tr> </table> <table> <tr> <td> Mom's mom </td> </tr> </table> </td> </tr> </table> </td> </tr> </table>
Output for 8.3.28
/bin/php-8.3.28: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.3.28) /bin/php-8.3.28: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.3.28) /bin/php-8.3.28: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.3.28) /bin/php-8.3.28: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.3.28)
Process exited with code 1.

preferences:
93.4 ms | 407 KiB | 5 Q