test - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of test.php
- <?php
- $tree = [
- 1 => ['value' => 1, 'children' => [2, 3]],
- 2 => ['value' => 2, 'children' => []],
- 3 => ['value' => 4, 'children' => [4,6]],
- 4 => ['value' => 8, 'children' => [5]],
- 5 => ['value' => 16, 'children' => []],
- 6 => ['value' => 32, 'children' => []],
- 7 => ['value' => 64, 'children' => [8]],
- 8 => ['value' => 100, 'children' => []],
- ];
- echo "Tree #1: ", sumTree($tree, 1), "\n";
- echo "Tree #3: ", sumTree($tree, 3), "\n";
- echo "Tree #7: ", sumTree($tree, 7), "\n";
- function sumTree(array $arr, int $id) {
- $sum = 0;
- $el = $arr[$id];
- $sum += $el['value'];
- foreach ($el['children'] as $childId) {
- $sum += sumTree($arr, $childId);
- }
- return $sum;
- /* your code here */
- //return rand(1, 10);
- }