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 ) : $original; } 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 ) : $original; } // 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.40.0100.00819.54
8.4.40.0100.01017.79
8.4.30.0070.01120.34
8.4.20.0160.00019.57
8.4.10.0070.01120.44
8.30.0070.00820.41
8.3.170.0100.01020.91
8.3.160.0040.01516.76
8.3.150.0040.01517.40
8.3.140.0110.00716.59
8.3.130.0060.00316.57
8.3.120.0080.01218.89
8.3.110.0040.00416.75
8.3.100.0030.00624.06
8.3.90.0100.01026.77
8.3.80.0000.00919.36
8.3.70.0000.01516.75
8.3.60.0190.00316.62
8.3.50.0120.00523.78
8.3.40.0110.00422.08
8.3.30.0060.01219.02
8.3.20.0070.00024.18
8.3.10.0040.00424.66
8.3.00.0000.00826.16
8.20.0060.00520.80
8.2.270.0100.01017.13
8.2.260.0000.01116.76
8.2.250.0080.00818.45
8.2.240.0090.00617.23
8.2.230.0000.00920.94
8.2.220.0070.00337.54
8.2.210.0030.00626.77
8.2.200.0030.00618.88
8.2.190.0060.00916.88
8.2.180.0140.00725.92
8.2.170.0150.00018.80
8.2.160.0110.00322.96
8.2.150.0040.00425.66
8.2.140.0070.00324.66
8.2.130.0080.00026.16
8.2.120.0040.00426.35
8.2.110.0060.00320.89
8.2.100.0080.00417.84
8.2.90.0040.00418.03
8.2.80.0000.00818.03
8.2.70.0030.00517.49
8.2.60.0050.00317.63
8.2.50.0040.00418.05
8.2.40.0080.00018.16
8.2.30.0040.00418.12
8.2.20.0000.00818.29
8.2.10.0000.00819.39
8.2.00.0040.00419.42
8.10.0040.00519.66
8.1.310.0030.00716.19
8.1.300.0040.00418.39
8.1.290.0030.00630.84
8.1.280.0110.01125.92
8.1.270.0030.00623.99
8.1.260.0000.00828.09
8.1.250.0080.00028.09
8.1.240.0070.00322.45
8.1.230.0070.00421.10
8.1.220.0000.00818.04
8.1.210.0000.00818.77
8.1.200.0000.00817.60
8.1.190.0000.00817.25
8.1.180.0060.00318.10
8.1.170.0080.00018.82
8.1.160.0030.00619.02
8.1.150.0030.00618.98
8.1.140.0040.00419.02
8.1.130.0030.00320.29
8.1.120.0000.00717.42
8.1.110.0060.00317.55
8.1.100.0040.00417.55
8.1.90.0080.00017.43
8.1.80.0000.00817.40
8.1.70.0000.00717.49
8.1.60.0030.00617.67
8.1.50.0080.00017.59
8.1.40.0080.00017.58
8.1.30.0000.00817.61
8.1.20.0040.00417.66
8.1.10.0000.00817.57
8.1.00.0040.00417.58
8.00.0040.00617.13
8.0.300.0000.00819.98
8.0.290.0040.00417.00
8.0.280.0070.00018.58
8.0.270.0000.01018.18
8.0.260.0000.00718.53
8.0.250.0000.00816.98
8.0.240.0040.00416.98
8.0.230.0050.00317.05
8.0.220.0000.00717.03
8.0.210.0000.00716.91
8.0.200.0000.00717.14
8.0.190.0040.00417.06
8.0.180.0040.00417.13
8.0.170.0030.00616.93
8.0.160.0050.00317.12
8.0.150.0040.00416.87
8.0.140.0000.00916.97
8.0.130.0040.00413.38
8.0.120.0060.00317.05
8.0.110.0030.00516.90
8.0.100.0000.00717.01
8.0.90.0000.00717.07
8.0.80.0100.00816.98
8.0.70.0020.00516.93
8.0.60.0000.00817.05
8.0.50.0000.00817.09
8.0.30.0170.00316.97
8.0.20.0140.00517.27
8.0.10.0030.00516.95
8.0.00.0060.01316.88
7.40.0070.00716.42
7.4.330.0050.00015.55
7.4.320.0000.00716.59
7.4.300.0000.00716.50
7.4.290.0040.00416.71
7.4.280.0080.00016.61
7.4.270.0020.00516.63
7.4.260.0000.00613.35
7.4.250.0000.00916.49
7.4.240.0040.00416.47
7.4.230.0040.00416.61
7.4.220.0020.00516.65
7.4.210.0070.01116.68
7.4.200.0040.00416.70
7.4.130.0080.01016.60
7.4.120.0110.01116.64
7.4.110.0140.00416.51
7.4.100.0100.00716.57
7.4.90.0160.00716.59
7.4.80.0160.00816.47
7.4.70.0060.00916.55
7.4.60.0150.00316.59
7.4.50.0090.00916.66
7.4.40.0090.01116.34
7.4.30.0120.00816.60
7.4.20.0060.01016.58
7.4.10.0060.01116.52
7.4.00.0120.00616.46
7.30.0080.00816.35
7.3.330.0050.00216.29
7.3.320.0000.00613.39
7.3.310.0000.00716.37
7.3.300.0030.00316.44
7.3.290.0110.00616.42
7.3.260.0110.00816.67
7.3.230.0000.01616.60
7.3.220.0060.00916.75
7.3.210.0060.01216.35
7.3.200.0070.01016.55
7.3.190.0090.00616.16
7.3.180.0130.00416.50
7.3.170.0070.01016.74
7.3.160.0150.00916.32
7.3.150.0040.01516.44
7.3.140.0060.01116.72
7.3.130.0030.01416.67
7.3.120.0160.00016.38
7.3.110.0090.00616.28
7.3.100.0090.00616.34
7.3.90.0140.00316.37
7.3.80.0130.00316.40
7.3.70.0060.00916.50
7.3.60.0080.00716.46
7.3.50.0120.00416.43
7.3.40.0070.01016.43
7.3.30.0140.00716.37
7.3.20.0110.00916.45
7.3.10.0060.01216.41
7.3.00.0070.01116.25
7.20.0100.00916.70
7.2.340.0060.01116.54
7.2.330.0100.01316.92
7.2.320.0000.01716.80
7.2.310.0120.00616.52
7.2.300.0060.01216.65
7.2.290.0170.00616.37
7.2.280.0070.01016.59
7.2.270.0030.01416.94
7.2.260.0080.00816.84
7.2.250.0100.00616.71
7.2.240.0090.00616.75
7.2.230.0060.01016.79
7.2.220.0160.00016.48
7.2.210.0070.01016.56
7.2.200.0130.00316.60
7.2.190.0000.01616.77
7.2.180.0080.00816.73
7.2.170.0070.01416.54
7.2.160.0100.01316.77
7.2.150.0090.00916.69
7.2.140.0090.00916.71
7.2.130.0090.00916.77
7.2.120.0140.00316.61
7.2.110.0070.01016.77
7.2.100.0160.00816.77
7.2.90.0280.00716.72
7.2.80.0080.00816.57
7.2.70.0060.01016.75
7.2.60.0130.00316.87
7.2.50.0130.00316.71
7.2.40.0030.01316.71
7.2.30.0060.01016.73
7.2.20.0100.00616.70
7.2.10.0190.00016.64
7.2.00.0110.01416.79

preferences:
34.51 ms | 407 KiB | 5 Q