6

In PHP i have the following code:

<?php
  echo "€<br>";
  echo ord("€") . "<br>";
  echo chr(128) . "<br>";

And i get the following output:

€
128
�

Why can't the chr function provide me the € sign? How can i get the €? I really need this to work. Thank's in advance.

BisaZ
  • 221
  • 1
  • 2
  • 9

1 Answers1

5

chr and ord only work with single byte ASCII characters. More specifically ord only looks at the first byte of its parameter.

The Euro sign is a three byte character in UTF-8: 0xE2 0x82 0xAC, so ord("€") (with the UTF-8 symbol) returns 226 (0xE2)

For characters that are also present in the ISO-8859-1 (latin1) character set, you can use utf8_encode() and utf8_decode(), but unfortunately the € sign is not contained there, so utf8_decode('€') will return "?" and cannot be converted back.

TL;DR: You cannot use ord and chr with a multi-byte encoding like UTF-8

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111