3v4l.org

run code in 300+ PHP versions simultaneously
<?php // create array of objects $arr = [new stdClass, new stdClass, new StdClass]; // assign properties for($i=0, $max=count($arr); $i < $max; $i++) { $arr[$i]->age = 0; $arr[$i]->creature = "squirrel"; } // update age values $arr[0]->age = 10; $arr[1]->age = 25; $arr[2]->age = 30; // recursively sum the ages of the array of objects ... function getTotalYears( $arr, $i, $carry ) { if ($i >= ( count( $arr ) - 1 ) ){ return $carry; } else { $carry += $arr[$i++]->age; getTotalYears( $arr, $i, $carry ); } } // .. and display result $total = getTotalYears( $arr, 0, 0); var_dump($total); echo "\nTotal years of nut foraging experience: $total";

preferences:
54.85 ms | 402 KiB | 5 Q