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, string $ver) { echo "\n--------------\nDOING IT WRONG: $message\n--------------\n"; } // ------------------------------------------------------------------ function apply_filters_typesafe( $tag, $arguments = array(), $value = null, ...$values ) { if ( empty( $GLOBALS[$tag] ) ) { return $value; } static $types_map; if (!$types_map) { $types_map = [ 'boolean' => 'boolean', 'integer' => 'numeric', 'double' => 'numeric', 'string' => 'string', 'array' => 'array', 'resource' => 'resource', 'resource (closed)' => 'resource', 'NULL' => 'mixed', ]; } $type = gettype( $value ); $is_object = is_object( $value ); $accepted_types = isset( $types_map[ $type ] ) ? array( $types_map[ $type ] ) : array(); // Do not calculate multiple times for same class. static $classes_types = []; // Skip calculation of accepted types if they are are explicitly passed. if ( $is_object && empty ( $arguments['accepted_types'] ) ) { $class = get_class( $value ); if ( isset( $classes_types[ $class ] ) ) { $accepted_types = $classes_types[ $class ]; } else { $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 ) ); } $classes_types[ $class ] = $accepted_types; } $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( $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 ) { if ( !$arguments['nullable'] ) { $filtered = $original; 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, $filtered, ...$values ) : $filtered; } static $functions; if ( ! $functions ) { $functions = array( 'int' => 'is_int', '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 ( (array)$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(); echo "\n----------------------\n"; // Now, let's try with more filters and nullable. add_filter('answer', function () { return null; }); add_filter('answer', function (int $previous = null) { return $previous ?? 21; }); add_filter('answer', function (int $previous = null) { return ':troll:'; }); add_filter('answer', function (int $previous = null) { return $previous === null ? null : $previous * 2; }); $answer = apply_filters_typesafe('answer', ['accepted_types' => ['int'], 'nullable' => true], 0); printf('The answer is: %d.', $answer);

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.20.0100.01019.70
8.4.10.0030.01220.55
8.3.150.0070.01317.29
8.3.140.0120.00620.99
8.3.130.0050.00318.52
8.3.120.0050.00520.57
8.3.110.0160.00320.94
8.3.100.0100.00024.06
8.3.90.0080.00026.77
8.3.80.0060.00319.36
8.3.70.0090.00618.57
8.3.60.0070.00716.75
8.3.50.0110.00923.73
8.3.40.0070.00721.90
8.3.30.0110.00418.88
8.3.20.0000.00724.18
8.3.10.0040.00424.66
8.3.00.0000.00826.16
8.2.270.0060.01317.43
8.2.260.0080.00316.51
8.2.250.0000.00816.82
8.2.240.0040.01217.19
8.2.230.0060.00922.58
8.2.220.0030.00637.54
8.2.210.0040.01126.77
8.2.200.0060.00318.88
8.2.190.0070.00718.13
8.2.180.0180.00025.92
8.2.170.0090.00619.08
8.2.160.0120.00322.96
8.2.150.0040.00425.66
8.2.140.0090.00024.66
8.2.130.0080.00026.16
8.2.120.0040.00421.17
8.2.110.0200.00021.12
8.2.100.0060.00620.98
8.2.90.0000.00818.00
8.2.80.0040.00418.86
8.2.70.0000.00818.29
8.2.60.0030.00617.63
8.2.50.0050.00317.62
8.2.40.0030.00618.47
8.2.30.0050.00318.22
8.2.20.0000.00718.09
8.2.10.0020.00519.32
8.2.00.0030.00519.46
8.1.310.0040.01120.23
8.1.300.0060.00317.98
8.1.290.0080.00430.84
8.1.280.0110.00725.92
8.1.270.0040.00423.99
8.1.260.0030.00626.35
8.1.250.0040.00428.09
8.1.240.0070.00319.39
8.1.230.0110.00019.29
8.1.220.0040.00417.89
8.1.210.0030.00618.77
8.1.200.0030.00617.60
8.1.190.0070.00317.48
8.1.180.0000.00818.10
8.1.170.0040.00418.85
8.1.160.0000.00718.90
8.1.150.0040.00418.89
8.1.140.0040.00419.03
8.1.130.0000.00917.55
8.1.120.0040.00417.54
8.1.110.0000.00917.43
8.1.100.0060.00317.56
8.1.90.0040.00417.52
8.1.80.0030.00517.45
8.1.70.0030.00617.55
8.1.60.0000.00917.56
8.1.50.0000.00817.64
8.1.40.0000.00917.55
8.1.30.0080.00017.74
8.1.20.0080.00017.73
8.1.10.0050.00317.60
8.1.00.0040.00417.62
8.0.300.0070.00018.77
8.0.290.0000.00916.88
8.0.280.0030.00518.52
8.0.270.0060.00318.07
8.0.260.0040.00417.04
8.0.250.0030.00317.00
8.0.240.0040.00417.11
8.0.230.0050.00217.12
8.0.220.0000.00716.94
8.0.210.0030.00316.94
8.0.200.0040.00417.10
8.0.190.0050.00217.06
8.0.180.0000.00816.96
8.0.170.0030.00617.10
8.0.160.0000.00717.15
8.0.150.0040.00416.91
8.0.140.0040.00416.97
8.0.130.0030.00313.44
8.0.120.0040.00416.95
8.0.110.0000.00717.07
8.0.100.0040.00416.85
8.0.90.0000.00817.09
8.0.80.0070.01117.08
8.0.70.0040.00417.05
8.0.60.0050.00317.09
8.0.50.0070.00017.12
8.0.30.0150.00317.07
8.0.20.0100.01217.30
8.0.10.0040.00417.02
8.0.00.0040.01417.11
7.4.330.0030.00415.55
7.4.320.0030.00516.63
7.4.300.0030.00316.67
7.4.290.0060.00316.73
7.4.280.0030.00616.71
7.4.270.0050.00216.59
7.4.260.0060.00013.40
7.4.250.0110.00016.72
7.4.240.0000.00716.55
7.4.230.0040.00416.54
7.4.220.0020.00516.66
7.4.210.0090.00716.64
7.4.200.0070.00016.70
7.4.130.0090.01116.65
7.4.120.0170.00416.73
7.4.110.0130.00716.64
7.4.100.0120.00616.08
7.4.90.0070.01016.64
7.4.80.0190.00016.36
7.4.70.0120.00616.69
7.4.60.0070.01116.36
7.4.50.0080.00916.62
7.4.40.0030.01316.39
7.4.30.0070.01016.34
7.4.20.0130.01016.36
7.4.10.0060.01116.23
7.4.00.0090.00916.70
7.3.330.0040.00416.46
7.3.320.0060.00013.45
7.3.310.0050.00316.45
7.3.300.0070.00016.49
7.3.290.0060.00916.44
7.3.260.0120.00916.51
7.3.230.0100.01016.50
7.3.220.0150.00916.41
7.3.210.0100.00716.61
7.3.200.0090.00916.48
7.3.190.0110.00716.42
7.3.180.0160.00816.42
7.3.170.0060.01216.54
7.3.160.0090.01216.50
7.3.150.0060.01216.45
7.3.140.0120.00616.42
7.3.130.0110.00616.26
7.3.120.0060.01316.34
7.3.110.0150.00016.31
7.3.100.0120.00816.21
7.3.90.0140.00316.61
7.3.80.0130.00616.39
7.3.70.0100.01316.43
7.3.60.0070.01016.51
7.3.50.0140.00316.29
7.3.40.0060.01016.51
7.3.30.0140.00516.34
7.3.20.0100.00716.38
7.3.10.0070.01116.34
7.3.00.0140.00616.34
7.2.340.0120.00616.68
7.2.330.0070.01116.67
7.2.320.0060.01216.45
7.2.310.0070.01116.83
7.2.300.0120.00516.73
7.2.290.0090.01216.43
7.2.280.0150.00516.83
7.2.270.0070.01016.79
7.2.260.0100.01016.82
7.2.250.0100.00716.93
7.2.240.0140.00316.59
7.2.230.0120.00616.86
7.2.220.0040.01316.40
7.2.210.0090.00916.60
7.2.200.0000.01716.61
7.2.190.0070.01016.71
7.2.180.0090.00916.65
7.2.170.0030.01316.50
7.2.160.0170.00216.71
7.2.150.0150.00616.75
7.2.140.0120.01316.65
7.2.130.0210.01216.66
7.2.120.0090.00916.72
7.2.110.0110.00816.70
7.2.100.0030.01416.72
7.2.90.0150.00316.59
7.2.80.0090.00916.58
7.2.70.0050.01316.71
7.2.60.0100.00716.48
7.2.50.0060.01216.64
7.2.40.0100.01016.78
7.2.30.0000.02016.64
7.2.20.0160.00616.91
7.2.10.0090.00916.55
7.2.00.0100.01316.59
7.1.330.0160.01115.50
7.1.320.0030.02215.44
7.1.310.0130.01015.62
7.1.300.0120.00815.60
7.1.290.0160.00415.44
7.1.280.0200.00015.59
7.1.270.0120.00715.57
7.1.260.0170.00915.63
7.1.250.0070.01415.50
7.1.240.0140.00715.51
7.1.230.0110.01115.31
7.1.220.0090.01015.66
7.1.210.0000.02015.54
7.1.200.0100.01215.49
7.1.190.0190.00315.55
7.1.180.0090.01315.55
7.1.170.0160.00515.43
7.1.160.0140.00615.47
7.1.150.0110.01115.56
7.1.140.0160.00815.55
7.1.130.0080.01615.41
7.1.120.0200.00315.48
7.1.110.0190.00315.47
7.1.100.0270.00015.38
7.1.90.0150.00715.41
7.1.80.0340.00315.58
7.1.70.0080.01515.59
7.1.60.0090.01315.60
7.1.50.0070.01415.53
7.1.40.0070.01515.49
7.1.30.0130.00815.76
7.1.20.0170.01015.62
7.1.10.0170.00715.57
7.1.00.0130.01115.48
7.0.330.0240.00915.30
7.0.320.0220.01015.39
7.0.310.0250.00415.39
7.0.300.0350.01215.44
7.0.290.0200.00815.27
7.0.280.0250.00615.48
7.0.270.0290.00015.36
7.0.260.0220.00615.45
7.0.250.0230.00715.43
7.0.240.0290.00615.32
7.0.230.0190.00615.28
7.0.220.0190.00915.37
7.0.210.0160.01215.56
7.0.200.0190.00815.43
7.0.190.0210.00515.34
7.0.180.0210.00615.48
7.0.170.0120.01515.43
7.0.160.0200.01015.41
7.0.150.0190.00815.39
7.0.140.0220.00615.63
7.0.130.0220.00615.43
7.0.120.0220.00315.50
7.0.110.0220.00815.34
7.0.100.0240.00315.37
7.0.90.0320.00315.42
7.0.80.0240.00815.32
7.0.70.0240.00515.45
7.0.60.0280.00015.29
7.0.50.0300.00015.16
7.0.40.0170.00915.29
7.0.30.0250.00615.23
7.0.20.0200.00715.33
7.0.10.0150.01215.33
7.0.00.0230.00815.27
5.6.400.0260.01215.80
5.6.390.0170.01015.54
5.6.380.0170.01115.87
5.6.370.0180.01115.86
5.6.360.0150.01215.66
5.6.350.0150.01515.88
5.6.340.0260.00415.66
5.6.330.0240.01015.73
5.6.320.0230.00916.01
5.6.310.0220.00715.93
5.6.300.0180.01215.56
5.6.290.0260.00315.86
5.6.280.0280.00015.78
5.6.270.0220.00615.70
5.6.260.0190.01115.67
5.6.250.0140.01415.65
5.6.240.0180.01115.96
5.6.230.0190.01515.80
5.6.220.0260.00815.63
5.6.210.0210.01815.57
5.6.200.0220.00715.70
5.6.190.0190.01315.83
5.6.180.0240.00915.46
5.6.170.0250.00915.80
5.6.160.0210.00915.68
5.6.150.0270.00715.80
5.6.140.0250.00615.98
5.6.130.0230.01215.85
5.6.120.0200.01015.97
5.6.110.0250.00815.87
5.6.100.0220.00815.59
5.6.90.0180.01215.44
5.6.80.0260.00315.52
5.6.70.0180.01115.54
5.6.60.0180.01115.77
5.6.50.0180.01015.75
5.6.40.0190.01115.68
5.6.30.0140.01215.62
5.6.20.0160.01315.57
5.6.10.0140.01415.63
5.6.00.0180.01215.80
5.5.380.0230.00915.77
5.5.370.0300.00015.70
5.5.360.0160.01315.68
5.5.350.0230.00715.50
5.5.340.0250.00515.64
5.5.330.0200.01115.52
5.5.320.0130.01615.79
5.5.310.0240.00615.61
5.5.300.0210.01015.70
5.5.290.0230.01215.52
5.5.280.0120.01815.58
5.5.270.0320.00015.80
5.5.260.0250.00415.50
5.5.250.0260.00415.47
5.5.240.0250.00515.46
5.5.230.0280.00415.49
5.5.220.0230.00815.68
5.5.210.0200.01115.54
5.5.200.0150.01415.41
5.5.190.0180.01415.40
5.5.180.0160.01315.68
5.5.170.0190.00915.75
5.5.160.0220.00915.58
5.5.150.0200.01215.42
5.5.140.0200.00915.65
5.5.130.0100.01715.56
5.5.120.0160.01215.34
5.5.110.0260.00415.40
5.5.100.0170.01015.67
5.5.90.0120.01615.34
5.5.80.0150.01215.66
5.5.70.0230.00515.58
5.5.60.0150.01215.54
5.5.50.0230.00615.54
5.5.40.0240.00515.57
5.5.30.0210.01015.68
5.5.20.0220.00715.60
5.5.10.0120.01615.73
5.5.00.0250.00315.46
5.4.450.0130.01612.59
5.4.440.0190.00912.24
5.4.430.0130.01012.41
5.4.420.0130.01312.30
5.4.410.0090.01512.42
5.4.400.0140.01012.26
5.4.390.0150.00712.38
5.4.380.0140.00812.50
5.4.370.0160.00712.39
5.4.360.0180.00712.27
5.4.350.0180.00612.31
5.4.340.0210.00212.36
5.4.330.0220.00012.35
5.4.320.0160.00812.44
5.4.310.0130.01012.53
5.4.300.0130.00812.54
5.4.290.0150.00612.47
5.4.280.0150.00612.32
5.4.270.0240.00012.30
5.4.260.0130.01012.32
5.4.250.0090.01212.32
5.4.240.0180.00512.26
5.4.230.0180.00312.30
5.4.220.0200.00312.23
5.4.210.0170.00612.24
5.4.200.0120.01212.45
5.4.190.0190.00512.47
5.4.180.0150.01012.33
5.4.170.0160.00812.57
5.4.160.0140.00712.33
5.4.150.0160.00912.32
5.4.140.0200.00312.33
5.4.130.0180.00312.48
5.4.120.0130.01012.40
5.4.110.0170.00912.36
5.4.100.0230.00312.58
5.4.90.0150.01112.36
5.4.80.0130.01412.34
5.4.70.0110.00912.34
5.4.60.0190.00312.28
5.4.50.0160.00512.31
5.4.40.0220.00612.34
5.4.30.0040.01912.47
5.4.20.0110.01112.41
5.4.10.0100.01412.46
5.4.00.0220.00412.38
5.3.290.0150.01112.93
5.3.280.0130.01012.80
5.3.270.0150.01212.77
5.3.260.0180.00712.71
5.3.250.0180.00712.86
5.3.240.0190.00312.76
5.3.230.0120.01212.72
5.3.220.0160.00812.95
5.3.210.0130.01312.70
5.3.200.0190.00412.82
5.3.190.0130.01312.91
5.3.180.0160.00812.93
5.3.170.0140.01112.85
5.3.160.0180.00312.98
5.3.150.0110.01213.03
5.3.140.0100.01612.96
5.3.130.0190.00512.93
5.3.120.0210.00512.92
5.3.110.0150.01512.87
5.3.100.0130.01312.92
5.3.90.0150.00812.98
5.3.80.0060.01612.74
5.3.70.0180.01112.73
5.3.60.0170.00712.91
5.3.50.0210.00312.87
5.3.40.0130.01112.71
5.3.30.0140.01012.86
5.3.20.0260.00012.88
5.3.10.0140.00912.63
5.3.00.0240.00612.68
5.2.170.0060.01211.75
5.2.160.0180.00311.86
5.2.150.0090.00911.82
5.2.140.0090.00912.03
5.2.130.0150.00611.74
5.2.120.0190.00011.89
5.2.110.0090.01211.80
5.2.100.0100.01311.80
5.2.90.0140.00411.74
5.2.80.0120.00711.74
5.2.70.0100.01011.75
5.2.60.0120.00911.55
5.2.50.0110.00711.87
5.2.40.0030.01412.00
5.2.30.0120.00511.66
5.2.20.0090.00911.71
5.2.10.0050.01411.75
5.2.00.0090.01211.37
5.1.60.0160.00410.88
5.1.50.0110.00510.81
5.1.40.0130.00510.98
5.1.30.0080.00811.16
5.1.20.0150.00310.99
5.1.10.0160.00410.84
5.1.00.0150.00310.86
5.0.50.0110.00810.81
5.0.40.0110.00410.81
5.0.30.0090.00610.81
5.0.20.0140.00610.81
5.0.10.0130.00410.81
5.0.00.0090.00410.81
4.4.90.0070.00210.81
4.4.80.0030.00610.81
4.4.70.0070.00310.81
4.4.60.0060.00310.81
4.4.50.0050.00510.81
4.4.40.0040.00410.81
4.4.30.0090.00010.81
4.4.20.0080.00410.81
4.4.10.0040.00410.81
4.4.00.0040.00410.81
4.3.110.0070.00210.81
4.3.100.0090.00010.81
4.3.90.0090.00010.81
4.3.80.0060.00310.81
4.3.70.0070.00210.81
4.3.60.0050.00510.81
4.3.50.0000.01010.81
4.3.40.0070.00210.81
4.3.30.0100.00010.81
4.3.20.0040.00410.81
4.3.10.0020.00410.81
4.3.00.0050.00210.81

preferences:
69.66 ms | 403 KiB | 5 Q