PHP ob_iconv_handler() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

Definition

In PHP, ob_iconv_handler() is an output buffer callback that converts the buffered output from the internal encoding to the output encoding.

You pass its name to ob_start(), and PHP converts the page when the buffer is flushed.

Syntax

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

PHP
ob_start("ob_iconv_handler");
CallWhat it does
ob_start("ob_iconv_handler")Starts a buffer that converts its output.
ob_end_flush()Sends the converted output.

The target comes from the iconv.output_encoding setting, or default_charset if that is empty.

Library declarations
PHP
ob_iconv_handler(string $contents, int $status): string

Parameters

ParameterDescription
contentsRequired. The buffered output.
statusRequired. A bit mask of PHP_OUTPUT_HANDLER_* flags. PHP sets this when the handler runs.

Return Value

Return ValueDescription
stringThe converted contents.

PHP Examples

Example 1 — Start the Handler

Use the handler with ob_start(). With the default UTF-8 settings the text does not change.

PHP
<?php
ob_start("ob_iconv_handler");

echo "Hello";

ob_end_flush();

How It Works

  1. Start a buffer. Pass the handler name as a string.
  2. Echo text. It goes into the buffer, not to the screen.
  3. Flush the buffer. The handler converts the text first.
  4. Both encodings are UTF-8, so the text stays the same.

Example 2 — Convert UTF-8 to Latin-1

Set the two encodings, then call the handler yourself and show the bytes.

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

ini_set("iconv.internal_encoding", "UTF-8");
ini_set("iconv.output_encoding", "ISO-8859-1");

$converted = ob_iconv_handler("café", PHP_OUTPUT_HANDLER_FINAL);

echo bin2hex($converted), PHP_EOL;

How It Works

  1. Hide the deprecation notices for the two settings.
  2. Set the input to UTF-8 and the output to ISO-8859-1.
  3. Call the handler with the text and a status flag.
  4. Show the bytes. e9 is é in ISO-8859-1.

Example 3 — List the Active Handler

Check that the handler is active.

PHP
<?php
ob_start("ob_iconv_handler");

$handlers = ob_list_handlers();

ob_end_clean();

print_r($handlers);

How It Works

  1. Start the buffer with the handler.
  2. Read the list with ob_list_handlers().
  3. Clean the buffer with ob_end_clean() so nothing extra is sent.
  4. Print the list. It shows the handler name.

Frequently Asked Questions

ob_iconv_handler() is an output buffer callback. It converts buffered output from the internal encoding to the output encoding.
ob_iconv_handler(string $contents, int $status): string. You normally pass its name to ob_start().
It comes from the iconv.output_encoding setting, or from default_charset when that setting is empty.
Yes. It sets the charset in the Content-Type header so the browser reads the converted text correctly.
No. If your whole page is UTF-8, no conversion is needed.

Back to iconv Functions

Browse all PHP iconv function tutorials.

iconv functions →

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