3v4l.org

run code in 300+ PHP versions simultaneously
<?php $mf = function () { /** * Used to store functions and allow recursive callbacks. * @var null|callback */ static $deploy = null; /** * Defined matches. * @var array */ static $ms = array(); /** * Dependency Injection callbacks, used for settings too. * @var array */ static $di = array(); // used to shorten code $num_args = func_num_args(); $get_args = func_get_args(); // used to retrieve currently defined matches // http://www.php.net/manual/en/regexp.reference.conditional.php // http://stackoverflow.com/questions/14598972/catch-all-regular-expression if ($num_args === 0 && PHP_SAPI !== 'cli') { return '/?(?!(' . implode('|', $ms) . ')$).*'; } elseif ($num_args === 2 && preg_match('#^[^/].+#', $get_args[0])) { // functions used for Dependency Injection and settings return $di[$get_args[0]] = $get_args[1]; } elseif ($num_args === 1 && preg_match('#^[^/].+#', $get_args[0])) { return is_callable($di[$get_args[0]]) ? call_user_func($di[$get_args[0]]) : $di[$get_args[0]]; } // functions have to be stored only once if (is_null($deploy) && PHP_SAPI === 'cli') { /** * Command line interface for the main function. * * @link http://php.net/manual/en/language.types.float.php * * @param callback $callback Function invoked when script ends * @param integer $priority Set `$callback` priority from 0 (high) to ~1.8e308 (low) * @return void */ $deploy = function ($callback, $priority = 0) use (&$deploy) { /** * Checking well formed call */ assert(is_callable($callback)); assert(is_numeric($priority)); /** * Arguments passed to the script. * @link http://php.net/manual/en/reserved.variables.argv.php * @var array */ $argv = $GLOBALS['argv']; if ($priority > 0) { /** * Recursion is used to set callback priority */ register_shutdown_function($deploy, $callback, $priority - 1); } else { $argv[0] = $callback; /** * register_shutdown_function is used to call added functions when script ends * @link http://it2.php.net/manual/en/function.register-shutdown-function.php */ call_user_func_array('register_shutdown_function', $argv); } }; } elseif (is_null($deploy)) { /** * Function used as a router. * * @link http://php.net/manual/en/language.types.float.php * * @param string $regex Regular expression used to match requested URL * @param callback $callback Function invoked when there's a match * @param string $method Request method(s) * @param float $priority Set `$callback` priority from 0 (high) to ~1.8e308 (low) * @return void */ $deploy = function ($regex, $callback, $method = 'GET', $priority = 0) use (&$deploy, &$ms) { /** * Checking well formed call */ assert(is_string($regex)); assert(is_callable($callback)); assert(is_string($method)); assert(is_numeric($priority)); // match stored as unique $ms[md5($regex)] = $regex; if ($priority > 0) { /** * Recursion is used to set callback priority */ register_shutdown_function($deploy, $regex, $callback, $method, $priority - 1); } elseif (preg_match('#' . $method . '#', $_SERVER['REQUEST_METHOD'])) { if (preg_match('#^' . $regex . '$#', $_SERVER['REQUEST_URI'], $matches)) { /** * Named subpatterns aren't allowed * @link http://it2.php.net/manual/en/regexp.reference.subpatterns.php */ while (list($key) = each($matches)) { if (!is_int($key)) { unset($matches[$key]); } } /** * Closure is added to `register_shutdown_function` calling */ if (isset($matches[0]) && $matches[0] === $_SERVER['REQUEST_URI']) { $matches[0] = $callback; } else { array_unshift($matches, $callback); } /** * register_shutdown_function is used to call added functions when script ends * @link http://it2.php.net/manual/en/function.register-shutdown-function.php */ call_user_func_array('register_shutdown_function', $matches); } } }; } return call_user_func_array($deploy, func_get_args()); }; assert_options(ASSERT_ACTIVE, 1); assert_options(ASSERT_BAIL, 1); $mf('/.*', function () { echo "Hello "; }); $mf('/.*', function () { echo "World!"; });
Output for 8.3.0 - 8.3.6
Deprecated: Constant ASSERT_ACTIVE is deprecated in /in/TYQMm on line 137 Deprecated: Function assert_options() is deprecated in /in/TYQMm on line 137 Deprecated: Constant ASSERT_BAIL is deprecated in /in/TYQMm on line 138 Deprecated: Function assert_options() is deprecated in /in/TYQMm on line 138 Fatal error: Uncaught AssertionError: assert(is_callable($callback)) in /in/TYQMm:52 Stack trace: #0 /in/TYQMm(52): assert(false, 'assert(is_calla...') #1 /in/TYQMm(134): {closure}('/.*', Object(Closure)) #2 /in/TYQMm(140): {closure}('/.*', Object(Closure)) #3 {main} thrown in /in/TYQMm on line 52
Process exited with code 255.
Output for 8.2.0 - 8.2.18
Fatal error: Uncaught AssertionError: assert(is_callable($callback)) in /in/TYQMm:52 Stack trace: #0 /in/TYQMm(52): assert(false, 'assert(is_calla...') #1 /in/TYQMm(134): {closure}('/.*', Object(Closure)) #2 /in/TYQMm(140): {closure}('/.*', Object(Closure)) #3 {main} thrown in /in/TYQMm on line 52
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Fatal error: Uncaught AssertionError: assert(is_callable($callback)) in /in/TYQMm:52 Stack trace: #0 /in/TYQMm(52): assert(false, 'assert(is_calla...') #1 /in/TYQMm(134): {closure}('/.*', Object(Closure)) #2 /in/TYQMm(142): {closure}('/.*', Object(Closure)) #3 {main} thrown in /in/TYQMm on line 52
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: assert(): assert(is_callable($callback)) failed in /in/TYQMm on line 52
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Warning: assert(): Assertion failed in /in/TYQMm on line 52
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/TYQMm on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/TYQMm on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/TYQMm on line 2
Process exited with code 255.

preferences:
230.63 ms | 401 KiB | 400 Q