3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // simulate network latency in seconds $seconds = 0.2; usleep($seconds * 1000000); function randomAverageSpeed() { return rand(5, 15); } // php 5.4 or later required to run this code class Runner { public $age, $distance, $time, $averageSpeed, $distanceKind; public function __construct($age, $distance, $time, $averageSpeed) { $this->age = $age; $this->distance = $distance; $this->time = $time; $this->averageSpeed = $averageSpeed; $this->distanceKind = $this->distanceKindConverter($distance); } private function distanceKindConverter($distanceAmount) { $distances = [ 5 => '', 10 => 'long', 7.195 => 'medium' ]; return $distances[$distanceAmount]; } } $team1 = [ 'Piet' => new Runner(39, 5, 0, randomAverageSpeed()), 'Ellen' => new Runner(19, 10, 0, randomAverageSpeed()), 'Jan' => new Runner(32, 5, 0, randomAverageSpeed()), 'Petra' => new Runner(27, 10, 0, randomAverageSpeed()), 'Kees' => new Runner(15, 5, 0, randomAverageSpeed()), 'Kate' => new Runner(18, 7.195, 0, randomAverageSpeed()) ]; $team2 = [ 'Klaas' => new Runner(46, 5, 0, randomAverageSpeed()), 'Beatrix' => new Runner(52, 10, 0, randomAverageSpeed()), 'Willem' => new Runner(34, 5, 0, randomAverageSpeed()), 'Alexander' => new Runner(30, 10, 0, randomAverageSpeed()), 'Maxima' => new Runner(44, 5, 0, randomAverageSpeed()), 'Amalia' => new Runner(26, 7.195, 0, randomAverageSpeed()) ]; $team3 = [ 'Huub' => new Runner(55, 5, 0, randomAverageSpeed()), 'Nelly' => new Runner(50, 10, 0, randomAverageSpeed()), 'Rene' => new Runner(43, 5, 0, randomAverageSpeed()), 'Tatjana' => new Runner(45, 10, 0, randomAverageSpeed()), 'Boris' => new Runner(28, 5, 0, randomAverageSpeed()), 'Wodan' => new Runner(23, 7.195, 0, randomAverageSpeed()) ]; $teams = [ 'Team 1' => $team1, 'Team 2' => $team2, 'Team 3' => $team3, ]; if (isset($_GET['getAllTeamData'])) { echo json_encode($teams); } if (isset($_GET['getLatestTeamDistanceData'])) { $teamsStats = [ 'Team 1' => rand(0, 1), 'Team 2' => rand(0, 1), 'Team 3' => rand(0, 1), ]; echo json_encode($teamsStats); } if (isset($_GET['getRunnerInfo'])) { $searchRunnerValue = trim($_GET['getRunnerInfo']); foreach ($teams as $key => $value) { foreach ($teams[$key] as $runner => $data) { if ($searchRunnerValue === $runner) { // remove some data unset($teams[$key][$runner]->distanceKind); unset($teams[$key][$runner]->time); unset($teams[$key][$runner]->averageSpeed); echo json_encode($teams[$key][$runner]); return; } } } if ($searchRunnerValue !== '') { echo 'ERROR: runner <strong>' . $searchRunnerValue . '</strong> not found!'; } else { echo 'ERROR: no runner name given!'; } }
Output for 5.4.0 - 7.1.0
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/viIRC on line 29
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/viIRC on line 29
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/viIRC on line 16
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/viIRC on line 16
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/viIRC on line 16
Process exited with code 255.