PHP iconv_set_encoding() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

Definition

In PHP, iconv_set_encoding() changes one of the iconv encoding settings: input, output or internal.

It has been deprecated since PHP 5.6. Set default_charset in php.ini instead.

Syntax

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

PHP
iconv_set_encoding("internal_encoding", "UTF-8");
CallWhat it does
iconv_set_encoding("internal_encoding", "UTF-8")Sets the internal encoding.
iconv_set_encoding("output_encoding", "UTF-8")Sets the output encoding.

Calling it raises an E_DEPRECATED notice. The examples hide it so the output stays clean.

Library declarations
PHP
iconv_set_encoding(string $type, string $encoding): bool

Parameters

ParameterDescription
typeRequired. input_encoding, output_encoding or internal_encoding.
encodingRequired. The character set name, such as UTF-8.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Set and Read the Internal Encoding

Change the internal encoding and read it back.

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

iconv_set_encoding("internal_encoding", "ISO-8859-1");

echo iconv_get_encoding("internal_encoding"), PHP_EOL;

How It Works

  1. Hide the deprecation notice. This keeps the output clean.
  2. Set the type and the name. internal_encoding is the type.
  3. Read it back with iconv_get_encoding().
  4. See the new value. It is ISO-8859-1.

Example 2 — See How It Changes iconv_strlen()

The internal encoding is the default for other iconv functions.

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

iconv_set_encoding("internal_encoding", "ISO-8859-1");
echo iconv_strlen("é"), PHP_EOL;

iconv_set_encoding("internal_encoding", "UTF-8");
echo iconv_strlen("é"), PHP_EOL;

How It Works

  1. Set the encoding to ISO-8859-1. Every byte now counts as one letter.
  2. The letter é is 2 bytes in UTF-8. So the first count is 2.
  3. Set the encoding back to UTF-8. The two bytes are one letter.
  4. The second count is 1. The setting changed the result.

Example 3 — Use default_charset Instead

The modern way is to set default_charset.

PHP
<?php
ini_set("default_charset", "UTF-8");

echo ini_get("default_charset"), PHP_EOL;
echo iconv_get_encoding("internal_encoding"), PHP_EOL;

How It Works

  1. Set default_charset. It is the setting PHP 5.6 and later read.
  2. Read it with ini_get().
  3. Read the iconv value. It follows default_charset.
  4. Prefer this way. It needs no deprecated function.

Frequently Asked Questions

iconv_set_encoding() changes the iconv input, output or internal encoding setting for the current script.
iconv_set_encoding(string $type, string $encoding): bool. Pass the type name and the encoding name.
Yes, since PHP 5.6. Use the default_charset setting instead.
The types are input_encoding, output_encoding and internal_encoding.
Use iconv_get_encoding() with the same type name.

Next: iconv_strlen()

iconv_strlen() counts the characters in a string, not the bytes.

iconv_strlen() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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