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";
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
***** Test with an array. **** ***** When succeeds **** array(1) { ["value"]=> string(4) "Test" } array(1) { ["blogname"]=> array(1) { ["value"]=> string(4) "Test" } } JSON = {"blogname":{"value":"Test"}} ***** When fails **** array(1) { [0]=> bool(false) } array(1) { [0]=> array(1) { [0]=> bool(false) } } JSON = [[false]]
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 ***** Test with an array. **** ***** When succeeds **** array(1) { ["value"]=> string(4) "Test" } array(1) { ["blogname"]=> array(1) { ["value"]=> string(4) "Test" } } JSON = {"blogname":{"value":"Test"}} ***** When fails **** array(1) { [0]=> bool(false) } array(1) { [0]=> array(1) { [0]=> bool(false) } } JSON = [[false]]

preferences:
169.5 ms | 402 KiB | 153 Q