1

I have stumbled upon this link. It's about chr function in PHP. There is an example in this link that I can't understand.

declare(encoding='UTF-8');
$str = chr(240) . chr(159) . chr(144) . chr(152);
echo $str;

This code gives "an elephant image" as an answer. I don't understand how this code above works? Are there any explanation about how the concatenation of chr works?

I will give more examples about this. I change the number from the last chr.

echo chr(240) . chr(159) . chr(144) . chr(154); // return a shell logo
echo chr(240) . chr(159) . chr(144) . chr(156); // return an ant logo
echo chr(240) . chr(159) . chr(144) . chr(158); // return a ladybug logo
echo chr(240) . chr(159) . chr(144) . chr(160); // return a fish logo
david
  • 3,225
  • 9
  • 30
  • 43

1 Answers1

3

The numbers written in hex are 0xF0 0x9F 0x90 0x98. This is the UTF-8 code for an elephant, see for example here.

Spoody
  • 2,852
  • 1
  • 26
  • 36
Karsten Koop
  • 2,475
  • 1
  • 18
  • 23
  • In the link that you provide, I notice that UTF-16 code for an elephant is `0xD83D 0xDC18`. Does these hexadecimal comes from the multiplication of four UTF-8 code? – david Jul 31 '18 at 08:40
  • No, it's a bit more complicated. In UTF-8, you can have between 1 to 4 bytes per symbol. If the highest bit of the first byte is set, a second byte follows, similar for second and third. UTF-16 is a different encoding with similar rules, but starting with two bytes. For details read for example [the articles on Wikipedia](https://en.wikipedia.org/wiki/UTF-8#Description). In the end, the encoded value is the unicode code point U+1F418. – Karsten Koop Jul 31 '18 at 08:46