3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { private $catTree = array(); public function addCategory($category, $parent) { if($parent == null){ if(count(array_column($this->catTree, 'category', $category)) > 0){ echo 'Duplicate category'; } else{ $temp = array("category" => $category,"parent" => $parent); array_push($this->catTree,$temp); } } else{ echo 'Parent: ' . $parent; print_r(count(array_column($this->catTree, 'category'), $parent)); } } public function getChildren($parent) { $that = $this; $keys = array_keys(array_column($this->catTree, 'parent'), $parent); return array_map(function($k) use ($that){return $that->catTree[$k]['category'];}, $keys); } } // For testing purposes (do not submit uncommented): $c = new CategoryTree; $c->addCategory('A', null); $c->addCategory('A', null); $c->addCategory('B', 'A'); $c->addCategory('B', 'D'); $c->addCategory('C', 'A'); echo implode(',', $c->getChildren('A'));
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
Duplicate categoryParent: A Fatal error: Uncaught TypeError: count(): Argument #2 ($mode) must be of type int, string given in /in/gkUgf:19 Stack trace: #0 /in/gkUgf(19): count(Array, 'A') #1 /in/gkUgf(37): CategoryTree->addCategory('B', 'A') #2 {main} thrown in /in/gkUgf on line 19
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Duplicate categoryParent: A Warning: count() expects parameter 2 to be int, string given in /in/gkUgf on line 19 Parent: D Warning: count() expects parameter 2 to be int, string given in /in/gkUgf on line 19 Parent: A Warning: count() expects parameter 2 to be int, string given in /in/gkUgf on line 19
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33
Duplicate categoryParent: A Warning: count() expects parameter 2 to be integer, string given in /in/gkUgf on line 19 Parent: D Warning: count() expects parameter 2 to be integer, string given in /in/gkUgf on line 19 Parent: A Warning: count() expects parameter 2 to be integer, string given in /in/gkUgf on line 19
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
Duplicate categoryParent: A Warning: count() expects parameter 2 to be long, string given in /in/gkUgf on line 19 Parent: D Warning: count() expects parameter 2 to be long, string given in /in/gkUgf on line 19 Parent: A Warning: count() expects parameter 2 to be long, string given in /in/gkUgf on line 19

preferences:
250.5 ms | 402 KiB | 294 Q