# New Functions `mb_chr()` and `mb_ord()`

> Section in “New and Extended Functionality › Internationalisation and Unicode” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/internationalisation-and-unicode/new-functions-mb_chr-and-mb_ord/>

The PHP function `chr()` creates a string with an ASCII character of the given value. `65` is the capital `A`, for example:

``` php
var_dump(chr(65));
```

``` php
string(1) "A"
```

The `ord()` function converts a single string character back to its numeric ASCII value:

``` php
var_dump(ord('A'));
```

    int(65)

Both functions, just like all other string-related PHP functions, do not work correctly on multibyte strings like UTF-8. Let us try a German special character, the `ß`:

``` php
$char = 'ß';

var_dump($char);
var_dump(ord($char));
```

As mentioned elsewhere in this book, `ß` is a non-ASCII character that is represented by two bytes, as the first `var_dump()` shows:

    string(2) "ß"
    int(195)

The string length is two, but the result of `ord()` is just one byte (a value between 0 and 255). Does not really make sense, does it? That is because PHP just looks at the first byte of the string, because it does not know about multibyte strings. You can also try

``` php
var_dump(chr(ord('ß')));
```

to see PHP fail.

To remedy this shortcoming, the multibyte equivalents of `ord()` and `chr()`, namely `mb_ord()` and `mb_chr()` have been introduced as part of the `mbstring` extension. Both require the encoding to be passed as second parameter, but since UTF-8 is assumed by default, we can also leave the second parameter out:

``` php
var_dump(mb_ord('ß'));
var_dump(mb_chr(mb_ord('ß')));
```

The result makes far more sense now:

    int(223)
    string(2) "ß"

And while we are at it:

``` php
var_dump('ß');
var_dump(mb_strlen('ß'));
```

This will result in:

    string(2) "ß"
    int(1)

Here you can directly see the difference between counting bytes and counting code points.

---

Previous: [Added Normalizer::getRawDecomposition() Method](https://php7explained.com/new-and-extended-functionality/internationalisation-and-unicode/added-normalizergetrawdecomposition-method/index.html.md) | Next: [New Function mb_scrub()](https://php7explained.com/new-and-extended-functionality/internationalisation-and-unicode/new-function-mb_scrub/index.html.md)
