3v4l.org

run code in 300+ PHP versions simultaneously
<?php $test = [ null, false, 1, 1.5, 'string', ['string in array without keys'], ['key' => 'string in array with keys'], [ 'multi-dim array' => 'string', 'sub-array' => [ 1, 2, 3] ], ]; foreach($test as $l10n) { echo PHP_EOL,'=======================', PHP_EOL; echo PHP_EOL,'Input',PHP_EOL; var_dump($l10n); $copy = $l10n; foreach ( (array) $l10n as $key => $value ) { if ( ! is_scalar( $value ) ) { continue; } $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); } $encoded1 = json_encode( $l10n ); echo PHP_EOL,'Original output',PHP_EOL; var_dump($encoded1); $l10n = $copy; if ( is_string( $l10n ) ) { $l10n = html_entity_decode( (string) $l10n, ENT_QUOTES, 'UTF-8' ); } else { $l10n = (array) $l10n; foreach ( $l10n as $key => $value ) { if ( ! is_scalar( $value ) ) { continue; } $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); } } $encoded2 = json_encode( $l10n ); echo PHP_EOL,'Patch Peter output',PHP_EOL; var_dump($encoded2); if ($encoded1 !== $encoded2) { echo "*** BC-break detected in patch!! ***", PHP_EOL; } $l10n = $copy; if ( ! is_array( $l10n ) ) { $l10n = html_entity_decode( (string) $l10n, ENT_QUOTES, 'UTF-8' ); } else { foreach ( $l10n as $key => $value ) { if ( ! is_scalar( $value ) ) { continue; } $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); } } $encoded3 = json_encode( $l10n ); echo PHP_EOL,'Patch Sergey output',PHP_EOL; var_dump($encoded3); if ($encoded1 !== $encoded3) { echo "*** BC-break detected in patch!! ***", PHP_EOL; } $l10n = $copy; if ( ! is_array( $l10n ) ) { // Throw the _doing_it_wrong_warning. $l10n = (array) $l10n; } foreach ( $l10n as $key => $value ) { if ( ! is_scalar( $value ) ) { continue; } $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); } $encoded4 = json_encode( $l10n ); echo PHP_EOL,'Patch 1 Juliette output',PHP_EOL; var_dump($encoded4); if ($encoded1 !== $encoded4) { echo "*** BC-break detected in patch!! ***", PHP_EOL; } $l10n = $copy; if ( ! is_array( $l10n ) ) { // Throw the _doing_it_wrong_warning. } if ( is_string( $l10n ) ) { $l10n = html_entity_decode( (string) $l10n, ENT_QUOTES, 'UTF-8' ); } else { foreach ( (array) $l10n as $key => $value ) { if ( ! is_scalar( $value ) ) { continue; } $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); } } $encoded5 = json_encode( $l10n ); echo PHP_EOL,'Patch 2 Juliette output',PHP_EOL; var_dump($encoded5); if ($encoded1 !== $encoded5) { echo "*** BC-break detected in patch!! ***", PHP_EOL; } }
Output for 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
======================= Input NULL Original output string(4) "null" Patch Peter output string(2) "[]" *** BC-break detected in patch!! *** Patch Sergey output string(2) """" *** BC-break detected in patch!! *** Patch 1 Juliette output string(2) "[]" *** BC-break detected in patch!! *** Patch 2 Juliette output string(4) "null" ======================= Input bool(false) Deprecated: Automatic conversion of false to array is deprecated in /in/ODNk5 on line 28 Original output string(4) "[""]" Patch Peter output string(4) "[""]" Patch Sergey output string(2) """" *** BC-break detected in patch!! *** Patch 1 Juliette output string(4) "[""]" Deprecated: Automatic conversion of false to array is deprecated in /in/ODNk5 on line 122 Patch 2 Juliette output string(4) "[""]" ======================= Input int(1) Fatal error: Uncaught Error: Cannot use a scalar value as an array in /in/ODNk5:28 Stack trace: #0 {main} thrown in /in/ODNk5 on line 28
Process exited with code 255.
Output for 8.0.0 - 8.0.30
======================= Input NULL Original output string(4) "null" Patch Peter output string(2) "[]" *** BC-break detected in patch!! *** Patch Sergey output string(2) """" *** BC-break detected in patch!! *** Patch 1 Juliette output string(2) "[]" *** BC-break detected in patch!! *** Patch 2 Juliette output string(4) "null" ======================= Input bool(false) Original output string(4) "[""]" Patch Peter output string(4) "[""]" Patch Sergey output string(2) """" *** BC-break detected in patch!! *** Patch 1 Juliette output string(4) "[""]" Patch 2 Juliette output string(4) "[""]" ======================= Input int(1) Fatal error: Uncaught Error: Cannot use a scalar value as an array in /in/ODNk5:28 Stack trace: #0 {main} thrown in /in/ODNk5 on line 28
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
======================= Input NULL Original output string(4) "null" Patch Peter output string(2) "[]" *** BC-break detected in patch!! *** Patch Sergey output string(2) """" *** BC-break detected in patch!! *** Patch 1 Juliette output string(2) "[]" *** BC-break detected in patch!! *** Patch 2 Juliette output string(4) "null" ======================= Input bool(false) Original output string(4) "[""]" Patch Peter output string(4) "[""]" Patch Sergey output string(2) """" *** BC-break detected in patch!! *** Patch 1 Juliette output string(4) "[""]" Patch 2 Juliette output string(4) "[""]" ======================= Input int(1) Warning: Cannot use a scalar value as an array in /in/ODNk5 on line 28 Original output string(1) "1" Patch Peter output string(5) "["1"]" *** BC-break detected in patch!! *** Patch Sergey output string(3) ""1"" *** BC-break detected in patch!! *** Patch 1 Juliette output string(5) "["1"]" *** BC-break detected in patch!! *** Warning: Cannot use a scalar value as an array in /in/ODNk5 on line 122 Patch 2 Juliette output string(1) "1" ======================= Input float(1.5) Warning: Cannot use a scalar value as an array in /in/ODNk5 on line 28 Original output string(3) "1.5" Patch Peter output string(7) "["1.5"]" *** BC-break detected in patch!! *** Patch Sergey output string(5) ""1.5"" *** BC-break detected in patch!! *** Patch 1 Juliette output string(7) "["1.5"]" *** BC-break detected in patch!! *** Warning: Cannot use a scalar value as an array in /in/ODNk5 on line 122 Patch 2 Juliette output string(3) "1.5" ======================= Input string(6) "string" Original output string(8) ""string"" Patch Peter output string(8) ""string"" Patch Sergey output string(8) ""string"" Patch 1 Juliette output string(10) "["string"]" *** BC-break detected in patch!! *** Patch 2 Juliette output string(8) ""string"" ======================= Input array(1) { [0]=> string(28) "string in array without keys" } Original output string(32) "["string in array without keys"]" Patch Peter output string(32) "["string in array without keys"]" Patch Sergey output string(32) "["string in array without keys"]" Patch 1 Juliette output string(32) "["string in array without keys"]" Patch 2 Juliette output string(32) "["string in array without keys"]" ======================= Input array(1) { ["key"]=> string(25) "string in array with keys" } Original output string(35) "{"key":"string in array with keys"}" Patch Peter output string(35) "{"key":"string in array with keys"}" Patch Sergey output string(35) "{"key":"string in array with keys"}" Patch 1 Juliette output string(35) "{"key":"string in array with keys"}" Patch 2 Juliette output string(35) "{"key":"string in array with keys"}" ======================= Input array(2) { ["multi-dim array"]=> string(6) "string" ["sub-array"]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } Original output string(48) "{"multi-dim array":"string","sub-array":[1,2,3]}" Patch Peter output string(48) "{"multi-dim array":"string","sub-array":[1,2,3]}" Patch Sergey output string(48) "{"multi-dim array":"string","sub-array":[1,2,3]}" Patch 1 Juliette output string(48) "{"multi-dim array":"string","sub-array":[1,2,3]}" Patch 2 Juliette output string(48) "{"multi-dim array":"string","sub-array":[1,2,3]}"
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/ODNk5 on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/ODNk5 on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/ODNk5 on line 2
Process exited with code 255.

preferences:
131.77 ms | 417 KiB | 5 Q