Functions utf8_encode and utf8_decode are Deprecated in PHP 8.2

Functions utf8_encode and utf8_decode are Deprecated in PHP 8.2

The PHP functions utf8_encode and utf8_decode are used for converting string between ISO-8859-1 and UTF-8 encodings. Since 8.2, these functions are deprecated because they have an inaccurate name and can cause confusion. Their names suggest a more general use, but functions only can be used for ISO-8859-1 and UTF-8 encodings conversion.

Since PHP 8.2, utf8_encode and utf8_decode functions emits a deprecation warning:

<?php

$utf8 = utf8_encode("\xa5\xa7\xb5"); // ISO-8859-1 -> UTF-8
$iso88591 = utf8_decode($utf8);      // UTF-8 -> ISO-8859-1
Deprecated: Function utf8_encode() is deprecated in main.php on line 3
Deprecated: Function utf8_decode() is deprecated in main.php on line 4

The example can be rewritten using mb_convert_encoding function:

<?php

$utf8 = mb_convert_encoding("\xa5\xa7\xb5", 'UTF-8', 'ISO-8859-1'); // ISO-8859-1 -> UTF-8
$iso88591 = mb_convert_encoding($utf8, 'ISO-8859-1', 'UTF-8');      // UTF-8 -> ISO-8859-1

Leave a Comment

Cancel reply

Your email address will not be published.