PHP mb_internal_encoding() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
mbstring

Definition

In PHP, mb_internal_encoding() reads or changes the default character encoding that the mb_* functions use.

Call it with no argument to read the setting, or pass an encoding name to change it.

Syntax

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

PHP
mb_internal_encoding("UTF-8");
CallWhat it does
mb_internal_encoding()Returns the current encoding, for example "UTF-8".
mb_internal_encoding("UTF-8")Sets the encoding and returns true.

The default comes from default_charset, which is UTF-8 on most servers.

Library declarations
PHP
mb_internal_encoding(?string $encoding = null): string|bool

Parameters

ParameterDescription
encodingOptional. The encoding name to set. If omitted, the current encoding is returned.

Return Value

Return ValueDescription
stringThe current encoding name when called with no argument.
booltrue on success or false on failure when you set an encoding.

PHP Examples

Example 1 — Read the Internal Encoding

Print the current setting. This is typical for a UTF-8 server.

PHP
<?php
echo mb_internal_encoding(), PHP_EOL;

How It Works

  1. Call it with no argument.
  2. It returns the current encoding name.
  3. On most servers this is UTF-8.
  4. Your server may show a different value if `php. ini` sets one.

Example 2 — Change the Setting

Set a new encoding and read it back.

PHP
<?php
var_dump(mb_internal_encoding("ISO-8859-1"));

echo mb_internal_encoding(), PHP_EOL;

How It Works

  1. Pass an encoding name. The function sets it.
  2. A successful set returns true.
  3. Call it again with no argument to read the new value.
  4. The setting lasts until the script ends.

Example 3 — See the Effect on mb_strlen()

The internal encoding is the default for other mb_* functions.

PHP
<?php
mb_internal_encoding("ISO-8859-1");
echo mb_strlen("é"), PHP_EOL;

mb_internal_encoding("UTF-8");
echo mb_strlen("é"), PHP_EOL;

How It Works

  1. Set ISO-8859-1. Every byte counts as one letter.
  2. The letter é is 2 bytes in UTF-8, so the count is 2.
  3. Set UTF-8 again. The two bytes are one letter.
  4. The count is 1. Always keep the setting equal to your text.

Frequently Asked Questions

mb_internal_encoding() reads or sets the default character encoding used by the mbstring functions.
mb_internal_encoding(?string $encoding = null): string|bool.
It comes from the default_charset setting, which is UTF-8 on most servers.
Not usually. If your text is UTF-8 and the default is UTF-8, you can leave it alone. Passing the encoding to each function is also safe.
No. It only sets the default for the mb_* functions. Send a Content-Type header to tell the browser the page encoding.

Next: mb_list_encodings()

mb_list_encodings() returns a list of the encodings mbstring supports.

mb_list_encodings() 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