3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Mocking WordPress function add_filter(string $tag, callable $value) { $GLOBALS[$tag][] = $value; } function _doing_it_wrong($fn, string $message) { echo "\n--------------\nDOING IT WRONG: $message\n--------------\n"; } // ------------------------------------------------------------------ function apply_filters_typesafe( $tag, $arguments = array(), $value = null, ...$values ) { if ( null === $value || empty( $GLOBALS[$tag] ) ) { return apply_filters( $tag, $value, ...$values ); } $type = gettype( $value ); $is_object = false; switch ( $type ) { case 'boolean': $accepted_types = array( 'boolean' ); break; case 'integer': case 'double': $accepted_types = array( 'numeric' ); break; case 'string': $accepted_types = array( 'string' ); break; case 'array': $accepted_types = array( 'array' ); break; case 'resource': case 'resource (closed)': $accepted_types = array( 'resource' ); break; case 'object': $is_object = true; default: $accepted_types = array(); } // Skip calculation of accepted types if they are are explicitly passed. if ( $is_object && empty ( $arguments['accepted_types'] ) ) { $class = get_class( $value ); $accepted_types = array( $class ); $parent = get_parent_class( $class ); while ( $parent ) { $accepted_types[] = $parent; $parent = get_parent_class( $parent ); } $accepted_types = array_merge( $accepted_types, class_implements( $class ) ); } $arguments = array_replace( array( 'nullable' => false, 'accepted_types' => $accepted_types ), $arguments ); $original = $value; // Objects are passed by ref, clone to return original unchanged in case of errors. $to_filter = $is_object ? clone $value : $value; $filter = array_shift($GLOBALS[$tag]); $filtered = $filter( $tag, $to_filter, ...$values ); // 'mixed' is a valid PHP 8 pseudo-type so we support for consistency. // That said, if mixed is fine then just use apply_filters. if ( in_array( 'mixed', (array)$arguments['accepted_types'] ) ) { return $GLOBALS[$tag] ? apply_filters_typesafe( $tag, $arguments, $filtered, ...$values ) : $filtered; } static $can_do_it_wrong = false; if ( ! $can_do_it_wrong && function_exists( '_doing_it_wrong' ) ) { $can_do_it_wrong = true; } if ( ( null === $filtered && !$arguments['nullable'] ) ) { if ( $can_do_it_wrong ) { _doing_it_wrong( __FUNCTION__, "Filters for $tag where not expected to return null.", '5.6' ); } return $GLOBALS[$tag] ? apply_filters_typesafe( $tag, $arguments, $original, ...$values ) : $filtered; } static $functions; if ( ! $functions ) { $functions = array( 'integer' => 'is_int', 'double' => 'is_float', 'float' => 'is_float', 'numeric' => 'is_numeric', 'number' => 'is_numeric', 'bool' => 'is_bool', 'boolean' => 'is_boolean', 'string' => 'is_string', 'array' => 'is_array', 'callable' => 'is_callable', 'function' => 'is_callable', 'resource' => 'is_resource', 'iterable' => 'is_iterable', 'countable' => 'is_countable', ); } foreach ( $arguments['accepted_types'] as $type ) { if ( isset( $functions[ $type ] ) && call_user_func( $functions[ $type ], $filtered ) ) { return $GLOBALS[$tag] ? apply_filters_typesafe( $tag, $arguments, $filtered, ...$values ) : $filtered; } if ( $is_object && is_string ( $type ) && is_a( $filtered, $type ) ) { return $GLOBALS[$tag] ? apply_filters_typesafe( $tag, $arguments, $filtered, ...$values ) : $filtered; } } if ( $can_do_it_wrong ) { $expected = implode( ', ', $arguments['accepted_types'] ); $actual = is_object( $filtered ) ? 'instance of ' . get_class($filtered) : gettype( $filtered ); _doing_it_wrong( __FUNCTION__, "Filters for $tag where expected to return a value of one of types $expected. Got $actual instead.", '5.6' ); } return $GLOBALS[$tag] ? apply_filters_typesafe( $tag, $arguments, $original, ...$values ) : $filtered; } // Let's try now to add some hooks, the first is bad, the second is good. add_filter('hello', function () { return 'This is not a callable' ; }); add_filter('hello', function () { return function () { return 'It works!'; }; }); // And lets' see if it works: $callable = apply_filters_typesafe('hello', ['accepted_types' => ['callable']], '__return_empty_string'); echo $callable();

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.0140.00720.11
8.4.150.0020.00014.05
8.4.140.0080.01317.49
8.4.130.0110.00917.68
8.4.120.0120.00824.33
8.4.110.0130.00717.81
8.4.100.0090.01218.80
8.4.90.0070.00518.79
8.4.80.0090.01118.86
8.4.70.0120.00917.66
8.4.60.0110.00918.55
8.4.50.0150.00620.47
8.4.40.0060.00317.71
8.4.30.0070.01320.54
8.4.20.0090.01219.54
8.4.10.0130.00620.71
8.3.280.0140.00718.43
8.3.270.0150.00516.55
8.3.260.0140.00616.43
8.3.250.0110.00818.95
8.3.240.0100.00917.04
8.3.230.0140.00617.13
8.3.220.0080.00718.96
8.3.210.0070.01116.93
8.3.200.0030.00516.88
8.3.190.0040.00419.02
8.3.180.0090.00616.99
8.3.170.0090.00918.80
8.3.160.0090.00916.55
8.3.150.0150.00417.13
8.3.140.0110.00416.88
8.3.130.0060.00318.72
8.3.120.0090.00920.68
8.3.110.0130.00320.94
8.3.100.0040.00424.06
8.3.90.0050.00326.77
8.3.80.0040.00419.36
8.3.70.0090.00916.88
8.3.60.0060.01618.43
8.3.50.0170.00623.62
8.3.40.0070.00721.91
8.3.30.0120.00618.97
8.3.20.0040.00424.18
8.3.10.0040.00424.66
8.3.00.0080.00026.16
8.2.290.0120.00720.47
8.2.280.0070.00918.55
8.2.270.0150.00417.55
8.2.260.0080.00018.18
8.2.250.0030.00618.62
8.2.240.0030.00617.39
8.2.230.0030.00622.58
8.2.220.0040.00837.54
8.2.210.0040.01126.77
8.2.200.0100.00018.88
8.2.190.0140.00416.58
8.2.180.0040.01125.92
8.2.170.0090.00618.96
8.2.160.0030.01022.96
8.2.150.0000.00825.66
8.2.140.0060.00324.66
8.2.130.0040.00426.16
8.2.120.0070.00026.35
8.2.110.0090.00020.37
8.2.100.0080.00318.03
8.2.90.0000.00818.03
8.2.80.0040.00421.14
8.2.70.0090.00017.50
8.2.60.0030.00517.63
8.2.50.0000.00818.05
8.2.40.0000.00818.03
8.2.30.0000.00918.07
8.2.20.0040.00418.24
8.2.10.0040.00419.43
8.2.00.0020.00519.27
8.1.330.0100.00922.21
8.1.320.0110.00916.23
8.1.310.0090.00016.33
8.1.300.0150.00320.06
8.1.290.0030.00630.84
8.1.280.0090.01225.92
8.1.270.0050.00323.99
8.1.260.0000.00828.09
8.1.250.0040.00428.09
8.1.240.0130.00320.79
8.1.230.0070.00422.33
8.1.220.0030.00517.89
8.1.210.0080.00018.91
8.1.200.0040.00417.48
8.1.190.0040.00417.23
8.1.180.0060.00318.10
8.1.170.0060.00318.79
8.1.160.0000.00918.88
8.1.150.0040.00419.02
8.1.140.0000.00718.88
8.1.130.0000.00720.24
8.1.120.0040.00417.53
8.1.110.0050.00317.59
8.1.100.0000.00717.55
8.1.90.0050.00317.53
8.1.80.0070.00017.57
8.1.70.0030.00617.40
8.1.60.0060.00317.55
8.1.50.0000.00817.52
8.1.40.0000.00817.59
8.1.30.0080.00017.68
8.1.20.0030.00517.63
8.1.10.0040.00417.66
8.1.00.0060.00317.46
8.0.300.0000.00719.98
8.0.290.0040.00417.00
8.0.280.0000.00718.48
8.0.270.0080.00018.14
8.0.260.0070.00020.19
8.0.250.0030.00317.07
8.0.240.0080.00017.05
8.0.230.0000.00717.11
8.0.220.0000.00717.07
8.0.210.0000.00716.91
8.0.200.0030.00517.11
8.0.190.0040.00417.10
8.0.180.0050.00316.95
8.0.170.0060.00316.93
8.0.160.0080.00016.94
8.0.150.0040.00416.86
8.0.140.0000.00916.96
8.0.130.0000.00613.45
8.0.120.0040.00716.95
8.0.110.0000.00717.09
8.0.100.0030.00617.05
8.0.90.0040.00416.91
8.0.80.0080.00816.96
8.0.70.0040.00417.00
8.0.60.0000.00816.89
8.0.50.0070.00016.93
8.0.30.0060.01217.26
8.0.20.0100.01017.14
8.0.10.0070.00017.05
8.0.00.0090.01716.61
7.4.330.0020.00215.55
7.4.320.0070.00016.59
7.4.300.0030.00316.68
7.4.290.0030.00316.68
7.4.280.0000.00816.59
7.4.270.0000.00716.61
7.4.260.0000.00513.38
7.4.250.0040.00416.45
7.4.240.0000.00716.54
7.4.230.0000.00816.75
7.4.220.0040.00416.53
7.4.210.0040.01216.59
7.4.200.0030.00316.47
7.4.130.0110.01316.63
7.4.120.0060.01216.50
7.4.110.0060.01016.70
7.4.100.0030.01516.36
7.4.90.0110.00816.52
7.4.80.0060.01116.25
7.4.70.0090.00916.25
7.4.60.0100.00716.46
7.4.50.0120.00616.16
7.4.40.0070.01116.29
7.4.30.0060.01216.43
7.4.20.0030.01416.58
7.4.10.0120.00916.71
7.4.00.0130.00916.40
7.3.330.0000.00716.18
7.3.320.0030.00313.36
7.3.310.0050.00316.48
7.3.300.0070.00016.30
7.3.290.0070.00916.40
7.3.260.0140.00416.38
7.3.230.0120.00616.60
7.3.220.0060.01216.66
7.3.210.0030.01616.68
7.3.200.0120.00616.33
7.3.190.0070.01016.45
7.3.180.0150.00316.54
7.3.170.0080.00816.71
7.3.160.0130.00316.41
7.3.150.0070.01016.64
7.3.140.0070.01116.68
7.3.130.0120.00616.59
7.3.120.0070.01116.47
7.3.110.0060.01816.49
7.3.100.0160.00316.34
7.3.90.0090.00916.25
7.3.80.0070.01116.29
7.3.70.0040.01416.45
7.3.60.0180.00616.46
7.3.50.0070.01016.36
7.3.40.0030.01316.29
7.3.30.0110.00816.28
7.3.20.0080.01116.33
7.3.10.0120.00616.26
7.3.00.0140.00316.50
7.2.340.0120.00616.41
7.2.330.0060.01216.66
7.2.320.0110.00716.82
7.2.310.0040.01416.25
7.2.300.0070.01116.58
7.2.290.0100.01016.56
7.2.280.0140.00316.89
7.2.270.0120.00416.61
7.2.260.0140.00516.52
7.2.250.0110.01116.88
7.2.240.0150.00316.74
7.2.230.0090.00916.62
7.2.220.0150.00316.81
7.2.210.0150.00316.69
7.2.200.0060.01816.52
7.2.190.0120.01216.67
7.2.180.0150.01216.49
7.2.170.0120.00916.54
7.2.160.0160.00316.64
7.2.150.0070.01216.74
7.2.140.0000.01816.78
7.2.130.0100.01016.63
7.2.120.0120.00616.60
7.2.110.0060.01216.71
7.2.100.0060.01316.74
7.2.90.0090.00916.69
7.2.80.0140.00616.57
7.2.70.0030.01616.44
7.2.60.0130.00616.70
7.2.50.0060.01316.67
7.2.40.0060.01316.52
7.2.30.0060.01216.81
7.2.20.0130.00616.42
7.2.10.0040.01416.69
7.2.00.0090.00916.86

preferences:
168.7 ms | 403 KiB | 5 Q