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 $value; } $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.4.120.0090.01120.28
8.4.110.0080.00417.90
8.4.100.0140.00517.91
8.4.90.0100.01020.79
8.4.80.0050.00319.06
8.4.70.0110.01218.86
8.4.60.0090.01220.50
8.4.50.0120.00818.89
8.4.40.0150.00617.73
8.4.30.0100.01020.45
8.4.20.0140.00317.91
8.4.10.0150.00420.52
8.3.250.0130.00616.62
8.3.240.0110.00916.54
8.3.230.0110.00816.58
8.3.220.0040.00319.08
8.3.210.0080.00916.84
8.3.200.0040.00516.79
8.3.190.0140.00517.19
8.3.180.0100.00819.07
8.3.170.0040.00417.11
8.3.160.0110.00717.26
8.3.150.0070.01118.31
8.3.140.0060.00316.75
8.3.130.0050.00317.03
8.3.120.0060.00319.22
8.3.110.0130.00320.94
8.3.100.0070.00324.06
8.3.90.0080.00026.77
8.3.80.0090.00016.75
8.3.70.0150.00016.88
8.3.60.0120.00916.88
8.3.50.0110.00823.63
8.3.40.0040.01121.90
8.3.30.0150.00018.79
8.3.20.0050.00324.18
8.3.10.0040.00424.66
8.3.00.0040.00426.16
8.2.290.0060.00616.88
8.2.280.0090.01118.54
8.2.270.0130.00718.46
8.2.260.0040.01517.33
8.2.250.0000.00816.77
8.2.240.0040.00418.86
8.2.230.0000.00922.58
8.2.220.0190.00037.54
8.2.210.0080.00026.77
8.2.200.0040.00416.50
8.2.190.0180.00416.75
8.2.180.0090.00625.92
8.2.170.0000.01418.96
8.2.160.0030.01022.96
8.2.150.0000.00825.66
8.2.140.0090.00024.66
8.2.130.0090.00026.16
8.2.120.0070.00026.35
8.2.110.0100.00019.18
8.2.100.0090.00318.03
8.2.90.0040.00417.88
8.2.80.0050.00518.91
8.2.70.0070.00317.63
8.2.60.0040.00417.63
8.2.50.0000.00818.05
8.2.40.0050.00318.00
8.2.30.0000.00818.25
8.2.20.0080.00018.06
8.2.10.0030.00519.32
8.2.00.0000.00719.43
8.1.330.0120.00716.09
8.1.320.0110.00718.07
8.1.310.0120.00618.65
8.1.300.0030.00716.09
8.1.290.0030.00618.88
8.1.280.0060.01225.92
8.1.270.0080.00023.99
8.1.260.0090.00028.09
8.1.250.0040.00428.09
8.1.240.0090.00323.91
8.1.230.0060.00620.99
8.1.220.0040.00418.64
8.1.210.0080.00018.77
8.1.200.0000.00817.35
8.1.190.0000.00817.35
8.1.180.0060.00318.10
8.1.170.0040.00418.60
8.1.160.0020.00518.95
8.1.150.0040.00418.91
8.1.140.0050.00218.88
8.1.130.0070.00020.24
8.1.120.0040.00417.42
8.1.110.0000.00817.52
8.1.100.0000.00817.51
8.1.90.0070.00017.57
8.1.80.0000.00817.51
8.1.70.0050.00317.54
8.1.60.0040.00417.57
8.1.50.0040.00417.55
8.1.40.0050.00317.63
8.1.30.0030.00617.74
8.1.20.0000.00817.65
8.1.10.0030.00617.65
8.1.00.0060.00317.60
8.0.300.0030.00620.10
8.0.290.0080.00016.88
8.0.280.0040.00418.63
8.0.270.0060.00318.07
8.0.260.0000.00720.16
8.0.250.0000.00717.12
8.0.240.0040.00417.11
8.0.230.0030.00317.00
8.0.220.0070.00016.93
8.0.210.0000.00717.04
8.0.200.0050.00317.02
8.0.190.0070.00017.13
8.0.180.0090.00016.99
8.0.170.0060.00317.11
8.0.160.0040.00417.05
8.0.150.0000.00716.97
8.0.140.0030.00616.89
8.0.130.0000.00513.48
8.0.120.0050.00516.93
8.0.110.0050.00317.05
8.0.100.0040.00417.12
8.0.90.0060.00317.09
8.0.80.0110.00717.00
8.0.70.0040.00416.94
8.0.60.0050.00317.01
8.0.50.0000.00716.91
8.0.30.0040.01517.23
8.0.20.0080.01117.16
8.0.10.0040.00417.14
8.0.00.0120.00616.95
7.4.330.0000.00515.55
7.4.320.0030.00316.55
7.4.300.0040.00416.70
7.4.290.0080.00016.71
7.4.280.0080.00016.69
7.4.270.0030.00316.57
7.4.260.0030.00313.29
7.4.250.0040.00716.54
7.4.240.0040.00416.65
7.4.230.0040.00416.57
7.4.220.0020.00616.65
7.4.210.0070.01016.60
7.4.200.0070.00316.60
7.4.130.0160.00716.53
7.4.120.0080.01716.68
7.4.110.0170.00316.70
7.4.100.0080.01116.66
7.4.90.0090.01416.73
7.4.80.0000.02416.38
7.4.70.0140.00316.34
7.4.60.0160.00316.68
7.4.50.0120.01216.72
7.4.40.0080.01216.56
7.4.30.0110.00716.53
7.4.20.0170.00616.69
7.4.10.0110.00616.56
7.4.00.0130.00316.84
7.3.330.0000.00716.27
7.3.320.0000.00513.21
7.3.310.0040.00416.24
7.3.300.0000.00716.32
7.3.290.0080.00716.41
7.3.260.0060.01616.55
7.3.230.0090.01216.39
7.3.220.0110.00816.77
7.3.210.0100.01016.38
7.3.200.0120.00916.38
7.3.190.0120.00616.43
7.3.180.0130.00316.44
7.3.170.0090.01516.47
7.3.160.0150.00416.75
7.3.150.0030.01516.40
7.3.140.0080.00816.40
7.3.130.0090.00916.48
7.3.120.0040.01516.51
7.3.110.0040.01316.58
7.3.100.0210.00516.61
7.3.90.0110.01116.61
7.3.80.0040.01416.42
7.3.70.0030.01616.32
7.3.60.0110.00716.39
7.3.50.0060.01216.39
7.3.40.0060.01116.56
7.3.30.0100.00716.50
7.3.20.0030.01416.42
7.3.10.0110.00816.38
7.3.00.0150.00616.45
7.2.340.0170.00716.80
7.2.330.0110.00916.87
7.2.320.0140.00516.48
7.2.310.0090.00916.32
7.2.300.0120.00916.57
7.2.290.0120.00616.54
7.2.280.0090.00916.90
7.2.270.0130.00316.57
7.2.260.0080.01116.90
7.2.250.0090.01316.80
7.2.240.0130.01016.81
7.2.230.0060.01716.91
7.2.220.0080.01616.68
7.2.210.0070.01016.73
7.2.200.0090.00916.62
7.2.190.0110.00816.71
7.2.180.0040.01416.64
7.2.170.0000.01816.56
7.2.160.0070.01116.66
7.2.150.0100.01016.81
7.2.140.0090.00916.62
7.2.130.0130.00616.80
7.2.120.0100.01016.70
7.2.110.0070.01116.75
7.2.100.0120.00616.80
7.2.90.0100.01016.79
7.2.80.0100.01016.53
7.2.70.0090.01316.70
7.2.60.0100.01316.91
7.2.50.0160.00616.68
7.2.40.0190.00316.78
7.2.30.0120.01616.87
7.2.20.0080.01316.66
7.2.10.0040.01816.73
7.2.00.0150.00616.66

preferences:
27.32 ms | 403 KiB | 5 Q