3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* A very simple implementation of 'typed array', like this: $stringArray = new TypedArray('string'); and now only strings can be added to the above $stringArray. Works with objects as well. More examples below. */ class TypedArray extends \ArrayIterator { protected $type = ''; public function __construct(string $type, array $input = [], int $flags = 0){ $this->type = $type; parent::__construct($input, $flags); } /* Just an utility funciton. In case it's a scalar (like int), or array etc - returns that if it's an object, returns the name of the class. */ public function getTypeOrClassName($val){ $testType = gettype($val); if($testType === 'object'){ return get_class($val); } return $testType; } public function checkType($val){ return $this->getTypeOrClassName($val) === $this->type; } public function getType(){ return $this->type; } public function offsetSet($offset, $value) { if(!$this->checkType($value)){ throw new \InvalidArgumentException('This TypedArray accepts only "' . $this->type . '" type, "' . $this->getTypeOrClassName($value) . '" given'); } return parent::offsetSet($offset, $value); } } ////////////////////////////////////////////////////////////////////////////// //examples $stringArray = new TypedArray('string'); $stringArray []= 'first string'; $stringArray []= 'second string'; //$stringArray [] = 2;//won't work - exception thrown var_dump((array)$stringArray); //tests with objects as values class User{ public $name; public function __construct($name){ $this->name = $name; } } $userArray = new TypedArray(User::class); $userArray[]= new User('John'); //$userArray []= new \StdClass();//exception thrown echo $userArray[0]->name;

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.00.0160.00720.05
8.4.150.0020.00014.05
8.4.140.0150.00617.87
8.4.130.0100.00619.60
8.4.120.0090.01022.24
8.4.110.0060.00318.72
8.4.100.0130.00717.80
8.4.90.0120.00918.00
8.4.80.0090.01118.14
8.4.70.0110.00819.78
8.4.60.0130.00820.09
8.4.50.0080.00918.59
8.4.40.0080.00318.05
8.4.30.0120.00817.97
8.4.20.0070.01419.62
8.4.10.0180.00419.67
8.3.280.0100.01118.45
8.3.270.0150.00516.86
8.3.260.0120.00816.50
8.3.250.0120.00818.93
8.3.240.0040.00516.66
8.3.230.0100.00816.64
8.3.220.0050.00317.28
8.3.210.0090.01216.69
8.3.200.0100.00916.83
8.3.190.0090.01017.03
8.3.180.0130.00816.76
8.3.170.0120.00618.87
8.3.160.0160.00317.16
8.3.150.0100.01017.11
8.3.140.0090.00916.67
8.3.130.0030.00516.75
8.3.120.0000.00820.74
8.3.110.0040.00820.94
8.3.100.0060.00324.06
8.3.90.0040.00426.77
8.3.80.0030.00618.43
8.3.70.0150.00317.00
8.3.60.0070.01418.62
8.3.50.0090.00718.38
8.3.40.0120.00318.79
8.3.30.0090.00618.81
8.3.20.0040.00421.73
8.3.10.0040.00423.61
8.3.00.0050.00323.48
8.2.290.0110.00820.34
8.2.280.0120.00716.95
8.2.270.0140.00417.22
8.2.260.0060.01216.81
8.2.250.0050.00316.88
8.2.240.0090.00017.39
8.2.230.0090.00922.58
8.2.220.0040.00937.54
8.2.210.0060.00926.77
8.2.200.0030.00616.63
8.2.190.0070.00717.00
8.2.180.0070.01016.63
8.2.170.0100.00722.96
8.2.160.0120.00320.13
8.2.150.0050.00324.18
8.2.140.0080.00024.66
8.2.130.0060.00321.02
8.2.120.0080.00026.35
8.2.110.0090.00022.25
8.2.100.0110.00017.91
8.2.90.0040.00419.41
8.2.80.0000.01018.21
8.2.70.0050.00318.16
8.2.60.0050.00317.63
8.2.50.0080.00018.16
8.2.40.0040.00420.57
8.2.30.0000.00720.65
8.2.20.0020.00518.14
8.2.10.0000.00818.13
8.2.00.0000.00718.03
8.1.330.0150.00518.33
8.1.320.0110.00816.06
8.1.310.0090.00016.92
8.1.300.0130.00618.42
8.1.290.0060.00330.84
8.1.280.0140.00725.92
8.1.270.0040.00424.66
8.1.260.0040.00426.35
8.1.250.0040.01128.09
8.1.240.0100.00019.32
8.1.230.0040.00720.89
8.1.220.0040.00417.78
8.1.210.0030.00618.77
8.1.200.0040.00417.35
8.1.190.0040.00417.22
8.1.180.0000.00918.10
8.1.170.0050.00318.59
8.1.160.0080.00018.83
8.1.150.0060.00320.34
8.1.140.0040.00417.75
8.1.130.0030.00319.04
8.1.120.0030.00617.38
8.1.110.0050.00217.35
8.1.100.0040.00417.51
8.1.90.0040.00417.53
8.1.80.0050.00317.55
8.1.70.0000.00717.45
8.1.60.0040.00417.51
8.1.50.0060.00317.57
8.1.40.0040.00417.50
8.1.30.0040.00417.72
8.1.20.0050.00317.67
8.1.10.0000.00817.58
8.1.00.0040.00417.38
8.0.300.0040.00419.89
8.0.290.0040.00416.75
8.0.280.0000.00718.41
8.0.270.0030.00317.20
8.0.260.0030.00316.73
8.0.250.0030.00316.88
8.0.240.0040.00416.98
8.0.230.0000.00716.90
8.0.220.0040.00416.85
8.0.210.0070.00016.94
8.0.200.0000.00617.00
8.0.190.0040.00417.03
8.0.180.0070.00017.00
8.0.170.0030.00616.91
8.0.160.0000.00716.95
8.0.150.0000.00716.75
8.0.140.0000.00716.87
8.0.130.0000.00613.41
8.0.120.0030.00916.87
8.0.110.0000.00816.88
8.0.100.0060.00316.89
8.0.90.0070.00016.73
8.0.80.0080.01116.90
8.0.70.0000.00716.78
8.0.60.0000.00716.81
8.0.50.0000.00716.81
8.0.30.0110.00917.26
8.0.20.0090.01017.40
8.0.10.0000.00716.84
8.0.00.0100.00816.72
7.4.330.0000.00515.55
7.4.320.0000.00716.64
7.4.300.0030.00316.43
7.4.290.0080.00016.66
7.4.280.0080.00016.64
7.4.270.0070.00016.54
7.4.260.0040.00416.60
7.4.250.0040.00416.61
7.4.240.0030.00616.53
7.4.230.0000.00716.51
7.4.220.0040.00416.52
7.4.210.0080.00616.68
7.4.200.0000.00716.52
7.4.160.0130.00816.47
7.4.140.0130.00917.86
7.4.130.0150.00816.50
7.4.120.0090.01016.46
7.4.110.0070.01016.48
7.4.100.0100.00716.64
7.4.90.0040.01316.58
7.4.80.0220.00019.39
7.4.70.0070.01016.50
7.4.60.0110.00816.53
7.4.50.0080.00816.39
7.4.40.0060.01016.35
7.4.00.0080.00815.06
7.3.330.0000.00613.32
7.3.320.0030.00313.21
7.3.310.0040.00416.48
7.3.300.0000.00716.25
7.3.290.0050.00916.41
7.3.280.0070.00816.43
7.3.260.0120.01016.57
7.3.250.0160.00516.36
7.3.240.0090.01016.45
7.3.230.0130.00316.30
7.3.210.0030.01416.40
7.3.200.0030.01716.45
7.3.190.0030.01316.58
7.3.180.0150.00316.43
7.3.170.0120.00916.58
7.3.160.0040.01216.57
7.2.330.0130.00616.77
7.2.320.0060.01616.53
7.2.310.0280.01916.52
7.2.300.0090.01516.63
7.2.290.0130.00816.93
7.2.60.0060.01017.12
7.2.30.0110.01718.60
7.2.20.0120.01518.98
7.2.10.0120.01319.05
7.2.00.0200.01418.84
7.1.200.0040.00415.90
7.1.150.0110.01417.69
7.1.140.0100.01417.85
7.1.130.0110.01217.75
7.1.120.0090.01417.85
7.1.110.0520.01017.37
7.1.100.0110.01417.38
7.1.90.0130.01117.09
7.1.80.0120.00917.18
7.1.70.0110.01216.28
7.1.60.0300.01934.08
7.1.50.0290.01633.93
7.1.40.0270.01733.59
7.1.30.0320.01733.59
7.1.20.0320.01633.71
7.1.10.0100.01415.63
7.1.00.0110.01115.88

preferences:
29.97 ms | 403 KiB | 5 Q