Home » How-To » How to Fix “Functions utf8_encode and utf8_decode are Deprecated in PHP 8.2”

How to Fix “Functions utf8_encode and utf8_decode are Deprecated in PHP 8.2”

The functions, “utf8_encode” and “utf8_decode” are used to convert strings from ISO-8859-1 to UTF-8 encodings.

They will always convert character codings because they won’t detect the actual character encoding in a text.

PHP contains both functions, but they cannot detect or convert other character codings like UTF-16 to UTF-8.

Due to misleading function names, little to no error messages, and no support, both the “utf8_encoide” and “utf8_decode” functions are deprecated in PHP 8.2.

How to Fix “Functions utf8_encode and utf8_decode are Deprecated in PHP 8.2”

To fix “Functions utf8_encode and utf8_decode are Deprecated in PHP 8.2”, you need to use the “mb_convert_encoding” function instead of the “utf8_encode” or “utf8_decode” functions.

You can also use the intl and iconv extensions to convert character encodings.

Problem: In PHP 8.2, the utf8_encode and utf8_decode functions are deprecated.

<?php

$utf8 = utf8_encode("\xa5\xa7\xb5"); // ISO-8859-2 -> UTF-8
$iso88592 = utf8_decode($utf8);      // UTF-8 -> ISO-8859-2

Using the utf8_encode and utf8_decode functions will give this warning:

Deprecated: Function utf8_encode() is deprecated in main.php on line 3
Deprecated: Function utf8_decode() is deprecated in main.php on line 4

Solution: Use the “mb_convert_encoding” function.

<?php

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

Further reading

How to install vcpkg on Ubuntu

How to install GCC 11 on Ubuntu

How to install jq on Ubuntu

About the author

Lim How Wei

Lim How Wei is the founder of followchain.org, with 8+ years of experience in Social Media Marketing and 4+ years of experience as an active investor in stocks and cryptocurrencies. He has researched, tested, and written thousands of articles ranging from social media platforms to messaging apps.

Lim has been quoted and referenced by major publications and media companies like WikiHow, Fast Company, HuffPost, Vice, New York Post, The Conversation, and many others. One of his articles about the gig economy was quoted by Joe Rogan who hosts The Joe Rogan Experience (arguably the most popular podcast in the world), in the This Past Weekend podcast by Theo Von.

In his free time, Lim plays multiple games like Genshin Impact, League of Legends, Counter-Strike, Hearthstone, RuneScape, and many others. He creates guides, walkthroughs, solutions, and more on games that he plays to help other players with their progression.