3v4l.org

run code in 300+ PHP versions simultaneously
<?php class URI implements \ArrayAccess, \Iterator, \Countable, \JsonSerializable { const SCHEME = 0x01; const USER = 0x02; const PASS = 0x04; const HOST = 0x08; const PORT = 0x10; const PATH = 0x20; const QUERY = 0x40; const FRAGMENT = 0x80; // The private props and magic methods are only implemented like this to give type validation, // these are effectively public properties private $scheme; private $user; private $pass; private $host; private $port; private $path; private $query; private $fragment; // obviously these two properties would be handled internally in a native impl private static $constPropMap = [ self::SCHEME => 'scheme', self::USER => 'user', self::PASS => 'pass', self::HOST => 'host', self::PORT => 'port', self::PATH => 'path', self::QUERY => 'query', self::FRAGMENT => 'fragment', ]; private $iterationPointer = self::SCHEME; private function validateScheme($value) { // in the generic URI syntax, only the format of the scheme is rigid return (bool) preg_match('/^[a-z][a-z0-9+.\-]*$/i', $value); } public function __construct($uri) { $parts = []; $this->query = new QueryLevel; if (((string) $uri) !== '' && false === $parts = parse_url($uri)) { throw new \InvalidArgumentException('Invalid URI'); } foreach ($parts as $name => $value) { $this->__set($name, urldecode($value)); } } public function __get($name) { if (!in_array($name, self::$constPropMap)) { trigger_error('Undefined property: ' . __CLASS__ . '::$' . $name, E_USER_NOTICE); return null; } return $this->$name; } public function __set($name, $value) { if ($value === null) { $this->$name = null; } else if ($name === 'port') { $this->port = (int) $value; } else if ($name === 'query') { parse_str($value, $query); $this->query = new QueryLevel($query); } else if (in_array($name, self::$constPropMap)) { if ($name === 'scheme' && !$this->validateScheme($value)) { throw new \InvalidArgumentException('Invalid URI scheme'); } $this->$name = (string) $value; } else { // because PHP allows expando properties on anything afaik :-( $this->$name = $value; } } public function __toString() { $result = ''; if (isset($this->scheme)) { $result = $this->scheme . ':'; } if (isset($this->host)) { $result .= '//'; if (isset($this->user)) { $result .= urlencode($this->user); if (isset($this->pass)) { $result .= ':' . urlencode($this->pass); } $result .= '@'; } $result .= urlencode($this->host); if (isset($this->port)) { $result .= ':' . $this->port; } } if (isset($this->path)) { $result .= implode('/', array_map('urlencode', explode('/', $this->path))); } if (!empty($this->query)) { $result .= '?' . $this->query; } if (isset($this->fragment)) { $result .= '#' . urlencode($this->fragment); } return $result; } /* ArrayAccess */ public function offsetExists($name) { return isset($this->$name) || isset(self::$constPropMap[$name]); } public function offsetGet($name) { if (isset(self::$constPropMap[$name])) { return $this->__get(self::$constPropMap[$name]); } else { return $this->__get($name); } } public function offsetSet($name, $value) { if (isset(self::$constPropMap[$name])) { $this->__set(self::$constPropMap[$name], $value); } else { $this->__set($name, $value); } } public function offsetUnset($name) { if (isset(self::$constPropMap[$name])) { $this->__set(self::$constPropMap[$name], null); } else if (in_array($name, self::$constPropMap)) { $this->__set($name, null); } else { unset($this->$name); } } /* Iterator */ public function current() { return $this->{self::$constPropMap[$this->iterationPointer]}; } public function key() { return self::$constPropMap[$this->iterationPointer]; } public function next() { $this->iterationPointer *= 2; } public function rewind() { $this->iterationPointer = self::SCHEME; } public function valid() { return $this->iterationPointer <= self::FRAGMENT; } /* Countable */ public function count() { $result = 0; foreach (self::$constPropMap as $const => $name) { if ($this->$name !== null) { $result++; } } return $result; } /* JsonSerializable */ public function jsonSerialize() { return $this->__toString(); } } class QueryLevel implements \ArrayAccess, \Iterator, \Countable { private $elements = []; private $iterationValid; private function encodeElement($name, $value, $nameFormat = '%s') { $result = null; if ($value !== null) { $name = sprintf($nameFormat, $name); if ($value instanceof QueryLevel) { $result = []; foreach ($value as $subName => $subValue) { $result[] = $this->encodeElement($subName, $subValue, $name . '[%s]'); } $result = implode('&', $result); } else { if (is_bool($value)) { $value = (int) $value; } $result = urlencode($name) . '=' . urlencode($value); } } return $result; } public function __construct($elements = []) { if (!is_array($elements) && !($elements instanceof QueryLevel)) { if (is_object($elements)) { $elements = get_object_vars($elements); } else { $elements = (array) $elements; } } foreach ($elements as $name => $value) { $this->__set($name, $value); } } public function __get($name) { if (!array_key_exists($name, $this->elements)) { trigger_error('Undefined property: ' . __CLASS__ . '::$' . $name, E_USER_NOTICE); return null; } return $this->elements[$name]; } public function __set($name, $value) { if (is_scalar($value) || $value === null) { $this->elements[$name] = $value; } else { $this->elements[$name] = new static($value); } } public function __toString() { $result = []; foreach ($this->elements as $name => $value) { if (!is_numeric($name) && null !== $encoded = $this->encodeElement($name, $value)) { $result[] = $encoded; } } return implode('&', $result); } /* ArrayAccess */ public function offsetExists($name) { return isset($this->elements[$name]); } public function offsetGet($name) { return $this->__get($name); } public function offsetSet($name, $value) { $this->__set($name, $value); } public function offsetUnset($name) { unset($this->elements[$name]); } /* Iterator */ public function current() { return current($this->elements); } public function key() { return key($this->elements); } public function next() { next($this->elements); } public function rewind() { reset($this->elements); } public function valid() { return key($this->elements) !== null; } /* Countable */ public function count() { return count($this->elements); } } $uri = new URI('http://www.google.com/'); $uri['path'] = '/whatever you want'; echo $uri;

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.0160.00718.80
8.3.50.0090.00918.27
8.3.40.0100.00619.39
8.3.30.0070.00719.19
8.3.20.0040.00420.46
8.3.10.0040.00422.05
8.3.00.0040.00422.43
8.2.180.0100.01017.00
8.2.170.0090.00622.96
8.2.160.0040.01221.06
8.2.150.0060.00324.18
8.2.140.0060.00324.66
8.2.130.0000.00826.16
8.2.120.0030.00519.61
8.2.110.0000.01022.11
8.2.100.0070.00718.03
8.2.90.0000.00819.33
8.2.80.0000.00818.17
8.2.70.0000.00917.88
8.2.60.0000.00818.05
8.2.50.0000.00818.07
8.2.40.0120.00018.34
8.2.30.0000.00818.20
8.2.20.0080.00017.86
8.2.10.0030.00518.11
8.2.00.0040.00418.06
8.1.280.0070.00725.92
8.1.270.0030.00623.92
8.1.260.0080.00026.35
8.1.250.0050.00328.09
8.1.240.0100.00023.73
8.1.230.0030.00919.05
8.1.220.0000.00817.78
8.1.210.0030.00518.77
8.1.200.0030.00617.60
8.1.190.0000.00817.43
8.1.180.0030.00618.10
8.1.170.0000.00818.77
8.1.160.0040.00422.14
8.1.150.0000.00818.81
8.1.140.0060.00317.72
8.1.130.0040.00417.97
8.1.120.0040.00417.63
8.1.110.0000.00717.67
8.1.100.0080.00017.76
8.1.90.0040.00417.61
8.1.80.0000.00717.69
8.1.70.0040.00417.61
8.1.60.0080.00417.74
8.1.50.0080.00017.68
8.1.40.0040.00417.82
8.1.30.0060.00317.80
8.1.20.0030.00517.94
8.1.10.0000.00817.82
8.1.00.0080.00017.91
8.0.300.0000.00718.77
8.0.290.0040.00417.17
8.0.280.0030.00318.63
8.0.270.0040.00417.33
8.0.260.0000.00717.01
8.0.250.0070.00017.20
8.0.240.0000.00717.19
8.0.230.0030.00717.21
8.0.220.0040.00417.16
8.0.210.0050.00217.12
8.0.200.0070.00017.23
8.0.190.0000.00917.22
8.0.180.0040.00417.18
8.0.170.0050.00317.14
8.0.160.0040.00417.13
8.0.150.0040.00417.07
8.0.140.0030.00516.94
8.0.130.0000.00813.54
8.0.120.0000.00817.20
8.0.110.0040.00417.15
8.0.100.0030.00616.97
8.0.90.0050.00217.20
8.0.80.0040.01217.12
8.0.70.0000.00817.18
8.0.60.0000.00817.03
8.0.50.0040.00417.05
8.0.30.0050.01417.33
8.0.20.0170.00217.52
8.0.10.0040.00417.07
8.0.00.0070.01216.95
7.4.330.0030.00315.00
7.4.320.0030.00616.80
7.4.300.0000.00616.94
7.4.290.0000.00816.91
7.4.280.0070.00016.81
7.4.270.0040.00416.84
7.4.260.0000.00716.88
7.4.250.0040.00416.79
7.4.240.0040.00416.89
7.4.230.0000.00816.73
7.4.220.0100.01016.83
7.4.210.0070.00916.94
7.4.200.0040.00416.56
7.4.190.0000.00717.01
7.4.160.0070.01016.87
7.4.150.0120.00617.40
7.4.140.0080.01117.86
7.4.130.0090.00916.89
7.4.120.0050.01516.84
7.4.110.0070.01516.96
7.4.100.0100.00716.85
7.4.90.0080.01216.99
7.4.80.0060.01816.84
7.4.70.0110.00716.68
7.4.60.0200.00016.66
7.4.50.0000.00716.87
7.4.40.0070.01022.77
7.4.30.0140.00616.88
7.4.00.0130.00315.39
7.3.330.0030.00413.36
7.3.320.0000.00513.45
7.3.310.0070.00016.51
7.3.300.0080.00016.70
7.3.290.0070.00716.53
7.3.280.0130.00716.59
7.3.270.0070.01717.40
7.3.260.0000.01716.54
7.3.250.0070.01116.61
7.3.240.0130.00317.03
7.3.230.0130.01016.70
7.3.210.0050.01416.92
7.3.200.0120.01219.39
7.3.190.0110.00616.61
7.3.180.0140.00316.96
7.3.170.0040.01816.97
7.3.160.0120.00616.69
7.3.120.0060.01214.87
7.2.330.0110.00716.94
7.2.320.0060.01217.07
7.2.310.0170.00816.85
7.2.300.0120.01216.96
7.2.290.0100.01116.88
7.2.00.0060.01019.52
7.1.100.0060.00618.41
7.1.70.0000.01117.44
7.1.60.0090.01619.82
7.1.50.0080.00417.11
7.1.00.0030.09722.46
7.0.200.0040.00416.99
7.0.140.0100.06722.07
7.0.110.0030.08721.88
7.0.100.0100.08021.78
7.0.90.0070.07021.75
7.0.80.0070.07721.66
7.0.70.0030.08021.74
7.0.60.0070.08321.74
7.0.50.0130.04021.80
7.0.40.0070.08019.79
7.0.30.0000.08719.64
7.0.20.0070.05719.70
7.0.10.0070.08719.64
7.0.00.0100.07719.71
5.6.280.0030.07321.21
5.6.260.0100.07320.72
5.6.250.0130.07320.79
5.6.240.0170.08020.63
5.6.230.0130.07320.75
5.6.220.0070.08020.71
5.6.210.0100.04720.62
5.6.200.0070.08720.77
5.6.190.0030.08720.65
5.6.180.0070.07020.68
5.6.170.0100.07320.72
5.6.160.0070.06720.77
5.6.150.0070.08320.71
5.6.140.0100.07720.68
5.6.130.0030.08020.52
5.6.120.0070.04720.70
5.6.110.0070.08320.68
5.6.100.0170.07320.60
5.6.90.0100.08320.90
5.6.80.0100.07720.00
5.6.70.0100.07320.00
5.6.60.0100.07320.00
5.6.50.0070.07020.04
5.6.40.0030.08020.05
5.6.30.0100.07320.05
5.6.20.0100.08020.00
5.6.10.0030.07720.06
5.6.00.0100.06320.01
5.5.380.0070.08020.45
5.5.370.0030.08020.50
5.5.360.0030.08720.60
5.5.350.0030.08020.22
5.5.340.0130.07320.83
5.5.330.0070.07320.98
5.5.320.0000.05020.97
5.5.310.0170.04021.05
5.5.300.0130.08021.08
5.5.290.0000.08320.85
5.5.280.0100.06720.94
5.5.270.0130.05721.01
5.5.260.0030.08720.87
5.5.250.0070.08320.85
5.5.240.0100.07720.29
5.5.230.0130.07020.23
5.5.220.0170.04320.39
5.5.210.0030.08720.19
5.5.200.0070.06320.21
5.5.190.0070.05020.30
5.5.180.0130.06320.29
5.5.160.0070.07320.26
5.5.150.0130.07020.30
5.5.140.0130.07020.39
5.5.130.0070.05320.38
5.5.120.0000.08020.33
5.5.110.0030.06720.32
5.5.100.0000.08320.22
5.5.90.0070.07320.27
5.5.80.0030.04720.32
5.5.70.0000.05720.06
5.5.60.0070.04320.20
5.5.50.0070.07020.20
5.5.40.0030.07020.26
5.5.30.0170.05020.26
5.5.20.0030.08020.20
5.5.10.0030.08020.30
5.5.00.0170.06020.07
5.4.450.0070.07719.50
5.4.440.0100.08019.46
5.4.430.0030.07319.48
5.4.420.0030.07319.29
5.4.410.0070.08019.30
5.4.400.0100.07019.23
5.4.390.0100.06718.96
5.4.380.0070.07019.02
5.4.370.0100.04319.18
5.4.360.0030.08318.98
5.4.350.0130.06719.16
5.4.340.0030.05018.95
5.4.320.0100.07019.27
5.4.310.0000.08319.23
5.4.300.0070.07719.09
5.4.290.0130.06318.95
5.4.280.0070.08019.31
5.4.270.0030.08019.00
5.4.260.0000.08019.17
5.4.250.0130.07319.23
5.4.240.0070.07018.98
5.4.230.0130.06719.16
5.4.220.0070.07719.16
5.4.210.0070.04019.00
5.4.200.0070.07318.94
5.4.190.0100.07719.21
5.4.180.0030.06718.99
5.4.170.0000.08019.00
5.4.160.0070.07719.14
5.4.150.0000.06718.99
5.4.140.0000.04016.41
5.4.130.0070.05716.39
5.4.120.0070.06016.41
5.4.110.0000.07716.60
5.4.100.0070.07716.62
5.4.90.0030.07316.46
5.4.80.0070.07716.39
5.4.70.0030.07716.46
5.4.60.0070.04316.35
5.4.50.0070.07016.47
5.4.40.0070.05716.58
5.4.30.0070.06716.30
5.4.20.0170.06316.38
5.4.10.0070.05716.44
5.4.00.0070.07016.04
5.3.290.0030.08014.81
5.3.280.0030.05714.60
5.3.270.0030.05314.63
5.3.260.0070.07014.60
5.3.250.0170.06014.70
5.3.240.0030.07014.59
5.3.230.0070.04014.70
5.3.220.0030.07314.53
5.3.210.0030.07714.42
5.3.200.0030.08014.60
5.3.190.0030.06714.69
5.3.180.0130.07014.57
5.3.170.0070.07314.52
5.3.160.0000.08014.70
5.3.150.0070.06714.69
5.3.140.0070.06314.57
5.3.130.0070.04714.54
5.3.120.0030.07314.57
5.3.110.0070.07714.63
5.3.100.0070.07314.00
5.3.90.0030.07014.04
5.3.80.0030.06714.04
5.3.70.0030.07013.97
5.3.60.0000.07714.01
5.3.50.0070.07013.97
5.3.40.0100.06313.94
5.3.30.0030.04014.13
5.3.20.0030.06713.75
5.3.10.0030.06013.63
5.3.00.0070.06713.64

preferences:
51.49 ms | 400 KiB | 5 Q