PHP utf8_encode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, utf8_encode() converts an ISO-8859-1 (Latin-1) string to UTF-8.

It is deprecated since PHP 8.2. Use mb_convert_encoding() or iconv() instead.

Syntax

Call utf8_encode() with the values you need. Start with this line.

PHP
$utf8 = utf8_encode($latin);
CallWhat it does
utf8_encode("caf\xE9")Converts the Latin-1 byte E9 into the UTF-8 letter é.
utf8_encode("café")Double-encodes text that is already UTF-8 and gives "café".

Deprecated since PHP 8.2 and shows a deprecation notice. Replace it with mb_convert_encoding($string, "UTF-8", "ISO-8859-1").

Library declarations
PHP
utf8_encode(string $string): string

Parameters

ParameterDescription
stringRequired. An ISO-8859-1 encoded string.

Return Value

Return ValueDescription
stringThe string encoded as UTF-8.

PHP Examples

Example 1 — Convert Latin-1 to UTF-8

A Latin-1 letter uses one byte. In UTF-8, accented letters use two. The deprecation notice is hidden here to keep the output clean.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

$latin = "caf\xE9";
$utf8  = utf8_encode($latin);

echo strlen($latin), PHP_EOL;
echo strlen($utf8), PHP_EOL;
echo $utf8, PHP_EOL;

How It Works

  1. Latin-1 string. \xE9 is the single byte for é in ISO-8859-1.
  2. Encode. utf8_encode() turns it into the two-byte UTF-8 form.
  3. Compare lengths. The string grows from 4 bytes to 5.
  4. Print it. A UTF-8 terminal shows café.

Example 2 — Double Encoding Problem

Running utf8_encode() on text that is already UTF-8 produces broken characters.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

$already = "café";

echo utf8_encode($already), PHP_EOL;

How It Works

  1. Already UTF-8. é is stored as two bytes.
  2. Wrong assumption. utf8_encode() treats each byte as a separate Latin-1 character.
  3. Double encoding. Each byte becomes its own UTF-8 character.
  4. Result. You see café instead of the real word.

Example 3 — Use mb_convert_encoding() Instead

The modern way gives the same bytes and no deprecation notice.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

$latin = "caf\xE9";
$old   = utf8_encode($latin);
$new   = mb_convert_encoding($latin, "UTF-8", "ISO-8859-1");

var_dump($old === $new);
echo $new, PHP_EOL;

How It Works

  1. Old way. utf8_encode() is deprecated.
  2. New way. Give mb_convert_encoding() the target, then the source encoding.
  3. Same result. The strict comparison is true.
  4. Print. Both give café.

Frequently Asked Questions

utf8_encode() converts an ISO-8859-1 (Latin-1) string into UTF-8.
string utf8_encode(string $string). It takes a Latin-1 string and returns a UTF-8 string.
Yes, since PHP 8.2. Use mb_convert_encoding($s, "UTF-8", "ISO-8859-1") or iconv() instead.
The input was already UTF-8, so it got encoded twice. Only use the conversion on real ISO-8859-1 data.
utf8_decode() converts UTF-8 back to ISO-8859-1. It is deprecated, too.

Next: vfprintf()

vfprintf() writes a formatted string to a stream, such as a file or STDOUT.

vfprintf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful