3v4l.org

run code in 500+ PHP versions simultaneously
<?php class RouteCollector { private array $routes; /* [path => [httpMethod => callback]] [ '/' => [ ['GET' => fn()], ['POST' => fn()] ] ] */ public function add(string $httpMethod, string $path, string|callable $callback): void { $this->routes[$path][$httpMethod] = $callback; } public function getRoutes(): array { return $this->routes; } } class RouteDispatcher { private string | \Closure $callback; # 404 Not Found public private(set) bool $isRouteFound = false; # 405 Method Not Allowed public private(set) bool $isMethodAllowed = false; public private(set) array $allowedMethods = []; public function __construct( private array $routes ){ } public function dispatch(string $httpMethod, string $uri): void { $result = $this->match($uri); # Found route verification if (!$result) { return; } $this->isRouteFound = true; # Method verification if (array_key_exists($httpMethod, $this->routes[$result])) { $this->isMethodAllowed = true; $this->callback = $this->routes[$result][$httpMethod]; } else { $this->allowedMethods = array_keys($this->routes[$result]); } } public function match(string $uri): ?string { $match = array_filter( $this->routes, fn($path): bool => $path === $uri, ARRAY_FILTER_USE_KEY ); return key($match); } public function getCallback(): string | \Closure { return $this->callback; } } /****************************************************************/ $router = new RouteCollector(); $router->add('GET', '/user/login', fn() => 'Form!'); $router->add('POST', '/user/login', fn() => 'Processing!'); $router->add('GET', '/hello', function () { return 'Hello ' . htmlspecialchars($_GET['user'] ?? '') .'!'; }); $router->add('POST', '/admin', fn() => 'Hello Admin!'); # 405 route $router->add('GET', '/', fn() => 'Hello World!'); $dispatcher = new RouteDispatcher($router->getRoutes()); $dispatcher->dispatch( 'PUT', '/user/login' ); echo match (true) { ! $dispatcher->isRouteFound => '404 Sorry, nothing here.', ! $dispatcher->isMethodAllowed => '405 Allowed is: ' . implode(', ', $dispatcher->allowedMethods), default => $dispatcher->getCallback()() };

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.90.0070.01317.08
8.5.80.0040.00417.04
8.5.70.0090.00816.77
8.5.60.0100.00816.61
8.5.50.0040.00316.89
8.4.240.0040.00619.75
8.4.230.0130.00819.65
8.4.220.0110.00923.95
8.4.210.0310.00919.57
8.4.90.0040.00419.57
8.3.320.0130.00618.44
8.3.310.0040.00518.41
8.3.180.0060.00218.48
8.3.50.0080.01118.49
8.2.320.0120.00918.14
8.2.310.0100.00918.06

preferences:
43.74 ms | 541 KiB | 5 Q