3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * WordPress Core functions. */ function wp_json_encode( $data, $options = 0, $depth = 512 ) { // Simulate JSON encoding failure to trigger the rest of the code. $json = false; //json_encode( $data, $options, $depth ); // If json_encode() was successful, no need to do more sanity checking. if ( false !== $json ) { return $json; } try { $data = _wp_json_sanity_check( $data, $depth ); } catch ( Exception $e ) { return false; } return json_encode( $data, $options, $depth ); } function _wp_json_sanity_check( $data, $depth ) { if ( $depth < 0 ) { throw new Exception( 'Reached depth limit' ); } if ( is_array( $data ) ) { $output = array(); foreach ( $data as $id => $el ) { // Don't forget to sanitize the ID! if ( is_string( $id ) ) { $clean_id = _wp_json_convert_string( $id ); } else { $clean_id = $id; } // Check the element type, so that we're only recursing if we really have to. if ( is_array( $el ) || is_object( $el ) ) { $output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 ); } elseif ( is_string( $el ) ) { $output[ $clean_id ] = _wp_json_convert_string( $el ); } else { $output[ $clean_id ] = $el; } } } elseif ( is_object( $data ) ) { $output = new stdClass; foreach ( $data as $id => $el ) { if ( is_string( $id ) ) { $clean_id = _wp_json_convert_string( $id ); } else { $clean_id = $id; } if ( is_array( $el ) || is_object( $el ) ) { $output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 ); } elseif ( is_string( $el ) ) { $output->$clean_id = _wp_json_convert_string( $el ); } else { $output->$clean_id = $el; } } } elseif ( is_string( $data ) ) { return _wp_json_convert_string( $data ); } else { return $data; } var_dump( $output ); return $output; } function _wp_json_convert_string( $string ) { // Simulate failed by returning false. if ( isset( $GLOBALS['simulate_fail'] ) ) { return false; } $encoding = mb_detect_encoding( $string, mb_detect_order(), true ); if ( $encoding ) { return mb_convert_encoding( $string, 'UTF-8', $encoding ); } else { return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' ); } } /** * Run some tests. */ echo "\n***** Test with an array. ****\n"; $data = array( 'blogname' => array( 'value' => 'Test', ), ); echo "\n***** When succeeds ****\n"; $json = wp_json_encode( $data ); echo "JSON = {$json}\n"; echo "\n***** When fails ****\n"; $GLOBALS['simulate_fail'] = true; $json = wp_json_encode( $data ); echo "JSON = {$json}\n";

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.3.60.0070.01016.63
8.3.50.0110.00416.64
8.3.40.0040.01618.89
8.3.30.0140.00018.57
8.3.20.0030.00624.18
8.3.10.0070.00024.66
8.3.00.0080.00026.16
8.2.180.0120.00325.92
8.2.170.0110.00419.09
8.2.160.0120.00322.96
8.2.150.0080.00025.66
8.2.140.0080.00024.66
8.2.130.0070.00326.16
8.2.120.0060.00319.64
8.2.110.0100.00720.37
8.2.100.0040.00817.91
8.2.90.0000.00817.88
8.2.80.0060.00318.73
8.2.70.0040.00418.16
8.2.60.0000.00818.22
8.2.50.0000.00818.10
8.2.40.0040.00421.11
8.2.30.0040.00419.33
8.2.20.0000.00818.21
8.2.10.0000.00821.09
8.2.00.0000.00719.32
8.1.280.0090.00925.92
8.1.270.0080.00023.99
8.1.260.0050.00326.35
8.1.250.0030.00628.09
8.1.240.0030.00624.02
8.1.230.0040.00719.72
8.1.220.0040.00417.79
8.1.210.0030.00618.77
8.1.200.0060.00317.38
8.1.190.0060.00317.34
8.1.180.0030.00618.10
8.1.170.0000.00818.59
8.1.160.0070.00318.98
8.1.150.0040.00418.91
8.1.140.0060.00318.99
8.1.130.0000.00817.54
8.1.120.0040.00417.57
8.1.110.0030.00517.55
8.1.100.0070.00017.45
8.1.90.0050.00217.51
8.1.80.0030.00717.57
8.1.70.0040.00417.54
8.1.60.0040.00417.59
8.1.50.0030.00517.62
8.1.40.0000.00817.50
8.1.30.0040.00417.69
8.1.20.0030.00517.74
8.1.10.0000.00817.63
8.1.00.0040.00417.60
8.0.300.0040.00418.77
8.0.290.0030.00616.75
8.0.280.0030.00518.45
8.0.270.0000.00716.91
8.0.260.0040.00416.91
8.0.250.0000.00716.98
8.0.240.0040.00417.04
8.0.230.0050.00317.05
8.0.220.0050.00216.99
8.0.210.0000.00716.88
8.0.200.0030.00317.01
8.0.190.0000.00917.07
8.0.180.0080.00016.95
8.0.170.0060.00316.97
8.0.160.0040.00416.92
8.0.150.0030.00516.90
8.0.140.0040.00416.88
8.0.130.0060.00013.38
8.0.120.0060.00317.06
8.0.110.0020.00516.98
8.0.100.0040.00417.03
8.0.90.0030.00517.00
8.0.80.0070.01217.00
8.0.70.0000.00716.95
8.0.60.0040.00416.93
8.0.50.0040.00416.87
8.0.30.0040.01216.96
8.0.20.0090.00617.08
8.0.10.0090.00617.02
8.0.00.0090.00617.07
7.4.330.0000.00515.55
7.4.320.0050.00316.60
7.4.300.0030.00316.73
7.4.290.0070.00016.41
7.4.280.0040.00416.59
7.4.270.0050.00316.52
7.4.260.0030.00313.42
7.4.250.0030.00316.68
7.4.240.0040.00416.72
7.4.230.0030.00316.72
7.4.220.0020.00616.62
7.4.210.0040.01116.70
7.4.200.0000.00716.45
7.4.160.0040.01116.62
7.4.150.0080.00516.55
7.4.140.0040.01616.63
7.4.130.0000.01416.60
7.4.120.0040.01016.59
7.4.110.0060.00816.53
7.4.100.0060.00916.65
7.4.90.0070.00716.63
7.4.80.0110.00316.62
7.4.70.0030.01016.48
7.4.60.0120.00616.45
7.4.50.0060.00816.49
7.4.40.0070.00716.39
7.4.30.0040.01016.41
7.4.20.0060.00916.64
7.4.10.0030.01916.65
7.4.00.0070.00716.68
7.3.330.0000.00716.44
7.3.320.0000.00513.45
7.3.310.0030.00416.34
7.3.300.0000.00716.31
7.3.290.0110.00716.42
7.3.270.0070.00716.42
7.3.260.0110.00316.40
7.3.250.0030.01016.42
7.3.240.0000.01416.43
7.3.230.0110.00316.44
7.3.220.0110.00316.28
7.3.210.0070.00716.36
7.3.200.0000.01416.32
7.3.190.0080.00616.30
7.3.180.0140.00016.33
7.3.170.0150.00016.38
7.3.160.0080.00516.34
7.3.150.0070.00716.21
7.3.140.0070.00716.32
7.3.130.0080.00616.29
7.3.120.0060.00816.37
7.3.110.0100.00316.22
7.3.100.0070.00716.28
7.3.90.0090.00616.55
7.3.80.0080.00616.43
7.3.70.0030.01016.52
7.3.60.0070.00716.53
7.3.50.0000.01316.56
7.3.40.0070.00716.46
7.3.30.0070.00716.54
7.3.20.0050.00816.63
7.3.10.0070.00716.59
7.3.00.0070.00716.62

preferences:
50.92 ms | 400 KiB | 5 Q