3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = '😀'; echo '=== UTF-8 =============================', PHP_EOL; echo 'STRING: ', $string, PHP_EOL; echo 'CHARACTERS: ', mb_strlen($string), PHP_EOL; // 1 echo 'BYTE COUNT: ', mb_strlen($string, '8bit'), PHP_EOL; // 4 foreach(range(0, mb_strlen($string) - 1) as $index) { echo PHP_EOL; echo 'CHARACTER #', $index + 1, ' CODE POINT: ', '0x', dechex(mb_ord(mb_substr($string, $index, 1))), PHP_EOL; // [0x1f600] echo 'CHARACTER #', $index + 1, ' BYTE COUNT: ', mb_strlen(mb_substr($string, $index, 1), '8bit'), PHP_EOL; // 4 } echo '=======================================', PHP_EOL, PHP_EOL; $string = mb_convert_encoding($string, 'UTF-16', 'UTF-8'); echo '=== UTF-16 ============================', PHP_EOL; echo 'STRING: ', $string, PHP_EOL; echo 'CHARACTERS: ', mb_strlen($string), PHP_EOL; // should be 1, is 2 echo 'BYTE COUNT: ', strlen($string), PHP_EOL; // shold be 4, is 4 foreach(range(0, mb_strlen($string) - 1) as $index) { echo PHP_EOL; echo 'CHARACTER #', $index + 1, ' CODE POINT: ', '0x', dechex(mb_ord(mb_substr($string, $index, 1))), PHP_EOL; // should be [0x1f600], is [0x0, 0x0] echo 'CHARACTER #', $index + 1, ' BYTE COUNT: ', mb_strlen(mb_substr($string, $index, 1), '8bit'), PHP_EOL; // should be [4], is [2, 2] } echo '=======================================', PHP_EOL;
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 8.0.0 - 8.0.12, 8.1rc1 - rc3
=== UTF-8 ============================= STRING: 😀 CHARACTERS: 1 BYTE COUNT: 4 CHARACTER #1 CODE POINT: 0x1f600 CHARACTER #1 BYTE COUNT: 4 ======================================= === UTF-16 ============================ STRING: �=� CHARACTERS: 2 BYTE COUNT: 4 CHARACTER #1 CODE POINT: 0x0 CHARACTER #1 BYTE COUNT: 2 CHARACTER #2 CODE POINT: 0x0 CHARACTER #2 BYTE COUNT: 2 =======================================

preferences:
47.25 ms | 461 KiB | 5 Q