utf8_encode/utf8_decode Deprecated

Deprecated: Function utf8_encode() is deprecated since PHP 8.2

Quick Answer

utf8_encode() and utf8_decode() are deprecated in PHP 8.2 because they only handle Latin-1 encoding. Use mb_convert_encoding() with explicit encoding parameters instead.

Why This Happens

PHP 8.2 deprecated utf8_encode() and utf8_decode() because their names are misleading. They only convert between Latin-1 (ISO-8859-1) and UTF-8, but the names suggest general UTF-8 handling. Use mb_convert_encoding() which supports all encodings and makes the conversion explicit.

The Problem

$utf8 = utf8_encode($latin1String); // Deprecated in PHP 8.2
$latin1 = utf8_decode($utf8String); // Deprecated in PHP 8.2

The Fix

$utf8 = mb_convert_encoding($latin1String, 'UTF-8', 'ISO-8859-1');
$latin1 = mb_convert_encoding($utf8String, 'ISO-8859-1', 'UTF-8');

Step-by-Step Fix

  1. 1

    Find deprecated calls

    Search your codebase for utf8_encode() and utf8_decode() calls.

  2. 2

    Replace with mb_convert_encoding

    Replace utf8_encode($s) with mb_convert_encoding($s, 'UTF-8', 'ISO-8859-1') and utf8_decode($s) with mb_convert_encoding($s, 'ISO-8859-1', 'UTF-8').

  3. 3

    Verify the source encoding

    Confirm the source data is actually ISO-8859-1. If it uses a different encoding, specify the correct one.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free