3v4l.org

run code in 300+ PHP versions simultaneously
<?php class URL { /* * In the native implementation, all the properties would be coerced to the correct type when setting them * You can't do this in userland because of the query element - in order for the array elements to be writable * without overwriting the whole array, you cannot use accessors :-( */ /** * @var string */ public $scheme; /** * @var string */ public $user; /** * @var string */ public $pass; /** * @var string */ public $host; /** * @var int */ public $port; /** * @var string */ public $path; /** * @var array */ public $query = []; /** * @var string */ public $fragment; /** * Wrapper for parse_url() * * @param string $string * @return URL * @throws InvalidArgumentException */ public static function createFromString($string) { if (false === $parts = parse_url($string)) { throw new InvalidArgumentException($string . ' could not be parsed as a valid URL'); } return new static( isset($parts['scheme']) ? $parts['scheme'] : null, isset($parts['user']) ? $parts['user'] : null, isset($parts['pass']) ? $parts['pass'] : null, isset($parts['host']) ? $parts['host'] : null, isset($parts['port']) ? $parts['port'] : null, isset($parts['path']) ? $parts['path'] : null, isset($parts['query']) ? $parts['query'] : null, isset($parts['fragment']) ? $parts['fragment'] : null ); } /** * Resolve $target as a relative URL against $source, using the same rules as a browser, so for example * * $source = http://google.com/ $target = /foo result = http://google.com/foo * $source = http://google.com/foo $target = bar result = http://google.com/bar * $source = http://google.com/foo $target = http://google.com/baz result = http://google.com/baz * * @param string|URL $source * @param string|URL $target * @return URL */ public static function resolve($source, $target) { if (!($source instanceof static)) { $source = static::createFromString((string) $source); } if (!($target instanceof static)) { $target = static::createFromString((string) $target); } // returning the same instance sometimes but not others would be confusing $result = clone $target; if (!isset($target->scheme)) { // anything with a scheme is considered absolute if (isset($target->host)) { // similarly anything with a host is absolute, just add a scheme is we have one if (isset($source->scheme)) { $result->scheme = $source->scheme; } } else { // host/scheme portion not specified, inherit from source foreach (['scheme', 'user', 'pass', 'host', 'port'] as $prop) { if (isset($source->{$prop})) { $result->{$prop} = $source->{$prop}; } } if ($target->path[0] === '/') { // If the target path is absolute, canonicalize it and use it $resultPath = self::resolveCanonicalPathComponents($target->path); } else { // Target path is relative // First we resolve the source path to a canonical and remove the file name component $sourcePath = self::resolveCanonicalPathComponents($source->path); array_pop($sourcePath); // Now resolve the target path against the source $resultPath = self::resolveCanonicalPathComponents($target->path, $sourcePath); } $result->path = '/' . implode('/', $resultPath); // The query and fragment elements are not inheritable so we don't touch them } } return $result; } /** * Normalise a path, resolving empty, . and .. components, optionally against another path * * @param $path * @param array $target * @return array */ private static function resolveCanonicalPathComponents($path, array $target = []) { // strip empty components and resolve . and .. foreach (preg_split('#[\\\\/]+#', $path, -1, PREG_SPLIT_NO_EMPTY) as $component) { switch ($component) { case '.': // current directory - do nothing break; case '..': // up a level array_pop($target); break; default: array_push($target, $component); break; } } // add a trailing empty element if path refers to a directory $lastChar = $path[strlen($path) - 1]; if ($lastChar === '/' || $lastChar === '\\') { array_push($target, ''); } return $target; } /** * Constructor takes components as individual arguments * * @param string $scheme * @param string $user * @param string $pass * @param string $host * @param int $port * @param string $path * @param string|array $query * @param string $fragment */ public function __construct($scheme = null, $user = null, $pass = null, $host = null, $port = null, $path = null, $query = null, $fragment = null) { foreach (['scheme', 'user', 'pass', 'host', 'path', 'fragment'] as $stringProp) { if (${$stringProp} !== null) { $this->{$stringProp} = (string) ${$stringProp}; } } if ($port !== null) { $this->port = (int) $port; } if ($query !== null) { if (is_scalar($query)) { parse_str((string) $query, $queryParsed); } else { $queryParsed = (array) $query; } $this->query = $queryParsed; } } /** * Forms all non-null components into a URL * * @return string */ public function __toString() { $result = ''; if (isset($this->scheme)) { $result = $this->scheme . ':'; } if (isset($this->host)) { $result .= '//'; if (isset($this->user)) { $result .= $this->user; if (isset($this->pass)) { $result .= ':' . $this->pass; } $result .= '@'; } $result .= $this->host; if (isset($this->port)) { $result .= ':' . $this->port; } } if (isset($this->path)) { $result .= $this->path; } if (isset($this->query) && $this->query !== []) { $result .= '?' . http_build_query($this->query); } if (isset($this->fragment)) { $result .= '#' . $this->fragment; } return $result; } } $url = URL::createFromString('http://google.com/'); $url->query['foo'] = [1, 2, 3]; echo $url . "\n"; echo URL::resolve('http://google.com/', '/foo') . "\n"; echo URL::resolve('http://google.com/foo', 'bar/') . "\n"; echo URL::resolve('http://google.com/foo', 'http://google.com/baz') . "\n";

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.3.60.0100.00718.68
8.3.50.0100.01122.11
8.3.40.0130.00319.04
8.3.30.0110.01120.09
8.3.20.0070.00020.25
8.3.10.0050.00321.91
8.3.00.0040.00422.47
8.2.180.0090.00618.72
8.2.170.0150.00022.96
8.2.160.0070.00720.69
8.2.150.0050.00324.18
8.2.140.0060.00324.66
8.2.130.0060.00326.16
8.2.120.0060.00319.66
8.2.110.0090.00022.11
8.2.100.0070.00419.48
8.2.90.0080.00019.49
8.2.80.0040.00419.52
8.2.70.0030.00917.88
8.2.60.0000.00818.05
8.2.50.0000.01018.07
8.2.40.0040.00418.34
8.2.30.0050.00318.30
8.2.20.0040.00417.90
8.2.10.0040.00418.00
8.2.00.0040.00718.05
8.1.280.0040.01125.92
8.1.270.0060.00324.66
8.1.260.0090.00026.35
8.1.250.0000.00828.09
8.1.240.0040.00423.82
8.1.230.0080.00419.28
8.1.220.0030.00517.89
8.1.210.0040.00418.77
8.1.200.0000.01017.73
8.1.190.0000.00817.76
8.1.180.0040.00419.39
8.1.170.0050.00318.76
8.1.160.0040.00422.31
8.1.150.0000.00818.94
8.1.140.0030.00517.70
8.1.130.0030.00517.96
8.1.120.0070.00017.75
8.1.110.0060.00317.60
8.1.100.0040.00417.57
8.1.90.0040.00417.61
8.1.80.0040.00417.71
8.1.70.0000.00717.63
8.1.60.0040.00417.78
8.1.50.0050.00317.70
8.1.40.0080.00017.75
8.1.30.0080.00017.84
8.1.20.0040.00417.93
8.1.10.0000.00917.80
8.1.00.0000.01217.74
8.0.300.0070.00018.77
8.0.290.0040.00417.30
8.0.280.0040.00418.48
8.0.270.0080.00018.18
8.0.260.0030.00316.98
8.0.250.0000.00717.21
8.0.240.0030.00917.18
8.0.230.0030.00317.14
8.0.220.0030.00517.19
8.0.210.0040.00417.09
8.0.200.0070.00017.18
8.0.190.0050.00217.15
8.0.180.0000.00817.00
8.0.170.0000.00917.08
8.0.160.0000.00717.12
8.0.150.0040.00417.04
8.0.140.0000.00817.13
8.0.130.0060.00313.60
8.0.120.0030.00517.21
8.0.110.0030.00517.10
8.0.100.0000.00717.11
8.0.90.0040.00417.07
8.0.80.0040.01217.24
8.0.70.0080.00017.20
8.0.60.0050.00217.10
8.0.50.0040.00417.02
8.0.30.0090.00917.66
8.0.20.0130.00817.40
8.0.10.0000.00717.21
8.0.00.0110.01117.15
7.4.330.0030.00315.00
7.4.320.0030.00316.92
7.4.300.0080.00016.84
7.4.290.0030.00316.91
7.4.280.0030.00316.85
7.4.270.0030.00516.72
7.4.260.0040.00416.90
7.4.250.0050.00316.78
7.4.240.0040.00416.88
7.4.230.0030.00516.71
7.4.220.0060.01216.77
7.4.210.0070.00916.98
7.4.200.0080.00016.98
7.4.190.0040.00416.99
7.4.160.0060.01016.86
7.4.150.0150.00317.40
7.4.140.0140.00617.86
7.4.130.0030.01416.71
7.4.120.0210.00316.84
7.4.110.0130.01016.80
7.4.100.0110.00816.75
7.4.90.0230.00016.75
7.4.80.0090.00919.39
7.4.70.0090.00916.80
7.4.60.0110.00816.88
7.4.50.0040.00416.62
7.4.40.0150.00322.77
7.4.30.0060.01216.66
7.4.10.0000.01615.18
7.4.00.0060.00815.29
7.3.330.0000.00613.45
7.3.320.0060.00013.40
7.3.310.0030.00316.60
7.3.300.0030.00316.55
7.3.290.0050.00916.61
7.3.280.0070.01016.58
7.3.270.0140.00317.40
7.3.260.0160.00716.64
7.3.250.0090.00816.57
7.3.240.0140.00916.61
7.3.230.0110.00716.73
7.3.210.0060.01316.95
7.3.200.0110.01119.39
7.3.190.0080.00816.54
7.3.180.0100.01016.47
7.3.170.0080.01116.86
7.3.160.0100.00716.66
7.3.130.0060.01315.23
7.3.120.0040.01114.82
7.3.110.0070.01115.09
7.3.100.0000.01115.32
7.3.90.0040.01215.38
7.3.80.0040.00714.96
7.3.70.0080.00015.15
7.3.60.0100.00315.18
7.3.50.0040.00815.20
7.3.40.0060.00614.78
7.3.30.0070.00714.91
7.3.20.0070.01017.02
7.3.10.0170.00616.70
7.3.00.0050.00916.70
7.2.330.0060.01216.98
7.2.320.0070.01116.69
7.2.310.0120.01217.04
7.2.300.0040.01417.02
7.2.290.0100.00716.96
7.2.260.0130.00615.32
7.2.250.0170.00015.33
7.2.240.0000.01515.36
7.2.230.0120.00015.08
7.2.220.0030.00615.40
7.2.210.0030.00915.23
7.2.200.0060.00615.48
7.2.190.0030.00915.21
7.2.180.0120.00315.40
7.2.170.0080.00815.16
7.2.160.0070.00715.21
7.2.150.0060.00617.20
7.2.140.0140.00317.23
7.2.130.0190.00917.04
7.2.120.0200.00417.08
7.2.110.0110.00617.01
7.2.100.0350.00316.84
7.2.90.0170.01116.93
7.2.80.0350.00716.93
7.2.70.0430.00417.20
7.2.60.0200.00617.13
7.2.50.0220.00916.92
7.2.40.0200.01316.95
7.2.30.0260.00817.13
7.2.20.0290.00717.07
7.2.10.0120.01017.21
7.2.00.0120.01018.01
7.1.330.0060.00616.13
7.1.320.0030.00915.90
7.1.310.0040.00416.23
7.1.300.0120.00415.89
7.1.290.0070.00716.07
7.1.280.0090.00616.13
7.1.270.0100.00316.12
7.1.260.0060.00815.98
7.1.250.0210.00315.76
7.1.240.0000.01515.88
7.1.230.0000.01315.82
7.1.220.0070.00315.73
7.1.210.0040.00815.94
7.1.200.0190.00415.92
7.1.190.0050.00515.72
7.1.180.0030.01015.78
7.1.170.0040.00715.87
7.1.160.0030.00615.91
7.1.150.0040.01215.77
7.1.140.0000.00916.04
7.1.130.0060.00915.74
7.1.120.0060.00315.74
7.1.110.0030.00616.03
7.1.100.0050.00817.30
7.1.90.0060.00316.16
7.1.80.0040.00915.85
7.1.70.0030.00516.69
7.1.60.0090.01217.82
7.1.50.0090.01116.64
7.1.40.0030.00715.97
7.1.30.0030.01016.20
7.1.20.0030.00616.10
7.1.10.0000.01816.16
7.1.00.0030.02119.24
7.0.330.0030.00615.64
7.0.320.0050.00515.54
7.0.310.0100.00015.80
7.0.300.0030.00915.37
7.0.290.0000.00815.51
7.0.280.0060.00615.26
7.0.270.0040.00715.57
7.0.260.0060.00715.64
7.0.250.0100.00015.52
7.0.240.0060.00615.76
7.0.230.0060.01015.71
7.0.220.0030.00715.49
7.0.210.0040.01115.72
7.0.200.0070.00516.29
7.0.190.0000.01415.74
7.0.180.0090.00615.36
7.0.170.0060.00615.65
7.0.160.0030.00715.68
7.0.150.0070.00715.39
7.0.140.0020.04518.85
7.0.130.0030.00615.73
7.0.120.0090.00315.73
7.0.110.0030.01015.50
7.0.100.0270.03517.88
7.0.90.0290.04217.79
7.0.80.0330.04817.79
7.0.70.0330.03317.72
7.0.60.0280.03917.78
7.0.50.0270.03017.99
7.0.40.0120.04116.94
7.0.30.0170.03416.97
7.0.20.0070.04816.79
7.0.10.0070.04316.86
7.0.00.0080.04216.82
5.6.400.0030.01014.56
5.6.390.0060.01014.54
5.6.380.0100.00714.56
5.6.370.0100.00314.24
5.6.360.0100.00314.58
5.6.350.0070.00714.59
5.6.340.0060.00614.67
5.6.330.0000.01014.60
5.6.320.0080.00614.80
5.6.310.0060.00814.55
5.6.300.0030.01014.77
5.6.290.0070.01114.38
5.6.280.0050.04017.82
5.6.270.0060.00614.99
5.6.260.0040.01114.53
5.6.250.0080.04417.62
5.6.240.0050.03617.54
5.6.230.0030.05017.69
5.6.220.0090.04517.71
5.6.210.0050.03217.74
5.6.200.0000.05217.73
5.6.190.0110.03717.87
5.6.180.0060.04517.85
5.6.170.0050.04317.82
5.6.160.0100.04317.91
5.6.150.0070.04617.89
5.6.140.0100.04417.96
5.6.130.0090.04517.92
5.6.120.0060.04817.87
5.6.110.0100.04117.82
5.6.100.0080.04417.71
5.6.90.0080.04717.79
5.6.80.0080.03617.37
5.6.70.0050.04717.40
5.6.60.0020.05017.54
5.6.50.0060.04317.47
5.6.40.0050.04417.54
5.6.30.0100.04017.57
5.6.20.0030.03617.45
5.6.10.0030.04717.42
5.6.00.0090.04017.39
5.5.380.0050.03817.54
5.5.370.0040.04917.55
5.5.360.0080.04317.66
5.5.350.0030.04517.54
5.5.340.0070.04217.84
5.5.330.0120.04017.68
5.5.320.0040.04217.77
5.5.310.0080.03817.69
5.5.300.0030.04617.68
5.5.290.0060.04317.53
5.5.280.0120.03717.77
5.5.270.0050.04417.64
5.5.260.0030.04317.57
5.5.250.0080.04517.47
5.5.240.0120.02317.44
5.5.230.0070.03817.44
5.5.220.0140.03317.47
5.5.210.0050.04217.29
5.5.200.0150.04017.42
5.5.190.0050.04417.43
5.5.180.0080.04217.46
5.5.170.0060.00914.39
5.5.160.0050.04017.25
5.5.150.0030.04517.17
5.5.140.0020.04817.43
5.5.130.0060.04317.29
5.5.120.0020.03517.45
5.5.110.0050.04517.27
5.5.100.0080.04317.32
5.5.90.0080.04117.21
5.5.80.0120.03517.29
5.5.70.0070.04417.33
5.5.60.0100.04017.35
5.5.50.0050.03617.13
5.5.40.0040.04417.39
5.5.30.0070.04317.27
5.5.20.0080.03917.30
5.5.10.0020.04617.35
5.5.00.0000.04817.16
5.4.450.0060.03915.35
5.4.440.0120.02115.40
5.4.430.0120.03715.40
5.4.420.0050.04215.45
5.4.410.0050.04015.19
5.4.400.0090.03715.18
5.4.390.0070.04115.23
5.4.380.0040.04015.28
5.4.370.0070.04015.15
5.4.360.0050.04315.27
5.4.350.0070.03815.13
5.4.340.0060.04315.14
5.4.330.0060.00611.05
5.4.320.0030.03215.14
5.4.310.0030.04015.01
5.4.300.0060.04115.19
5.4.290.0070.04215.21
5.4.280.0070.03815.13
5.4.270.0060.02615.14
5.4.260.0090.03814.96
5.4.250.0030.04215.03
5.4.240.0110.03415.06
5.4.230.0030.03815.22
5.4.220.0050.04415.15
5.4.210.0030.02715.10
5.4.200.0070.04315.11
5.4.190.0100.03814.99
5.4.180.0040.04015.03
5.4.170.0070.04215.06
5.4.160.0050.04214.99
5.4.150.0110.03615.13
5.4.140.0100.04013.91
5.4.130.0060.04313.80
5.4.120.0110.03113.93
5.4.110.0080.03713.86
5.4.100.0060.02113.60
5.4.90.0020.04013.85
5.4.80.0030.03913.80
5.4.70.0020.03813.78
5.4.60.0030.04313.80
5.4.50.0080.03113.88
5.4.40.0100.03313.87
5.4.30.0080.02113.86
5.4.20.0070.02313.80
5.4.10.0100.03413.79
5.4.00.0050.04013.50
5.3.290.0050.03612.55
5.3.280.0030.04512.49
5.3.270.0020.04412.42
5.3.260.0050.02512.49
5.3.250.0100.03912.46
5.3.240.0080.03512.31
5.3.230.0050.03112.51
5.3.220.0070.04012.49
5.3.210.0150.03312.47
5.3.200.0040.03612.49
5.3.190.0070.03512.50
5.3.180.0020.04212.59
5.3.170.0070.04012.46
5.3.160.0060.02612.49
5.3.150.0100.03712.54
5.3.140.0100.03012.38
5.3.130.0030.04212.45
5.3.120.0020.04112.47
5.3.110.0030.04312.53
5.3.100.0060.03812.24
5.3.90.0040.03712.20
5.3.80.0070.02412.16
5.3.70.0030.04312.07
5.3.60.0060.03812.13
5.3.50.0070.03511.96
5.3.40.0070.03512.11
5.3.30.0070.03711.96
5.3.20.0070.03712.11
5.3.10.0110.03211.82
5.3.00.0080.03311.70

preferences:
65.57 ms | 400 KiB | 5 Q