Multibyte strings7 min readExamples run on PHP 8.5.10

mb_get_info()

Learn how mb_get_info() reports mbstring's current configuration, how to request one setting or all of them, and which mistakes to avoid.

Signature

mb_get_info(string $type = "all"): array|string|int|false|null
Available since PHP 4.2.07.48.08.18.28.38.48.5All versions →

Purpose

mb_get_info() is a read-only inspector for the multibyte string extension. It reports the settings mbstring is using right now: the internal encoding, the detection order, the character substituted for invalid input, the language and several others. It is useful when you need to confirm configuration before running multibyte code, or when you want a diagnostic dump while debugging character-set problems.

It does not change anything. To modify a setting you use the matching setter, such as mb_internal_encoding(), mb_detect_order() or mb_substitute_character(), and then use mb_get_info() to read the result back.

Signature

Parameters and defaults

$type selects what to report. The default is "all".

  • "all" (or no argument) returns an array with these entries: internal_encoding, http_input, http_output, http_output_conv_mimetypes, mail_charset, mail_header_encoding, mail_body_encoding, illegal_chars, encoding_translation, language, detect_order, substitute_character and strict_detection.
  • Any one of those names returns just that setting.

The manual's changelog notes that the old "func_overload" and "func_overload_list" types stopped being supported in PHP 8.0.0, so do not request them.

Return value and failure

With "all" you get an array keyed by the names above. With a single name you get the value for that setting, whose type depends on the setting:

  • a string, such as an encoding name;
  • an integer, such as a substitute character code point;
  • an array, such as the detection order;
  • false on failure, as documented. The declared return type also includes null.

Because the result type varies, do not assume a string. Check or normalise it before using it in string operations.

Example 1: reading the internal encoding

Set the encoding explicitly first so the output does not depend on the host configuration, then read it back.

Example
<?php
mb_internal_encoding('UTF-8');
echo json_encode(mb_get_info('internal_encoding')), "\n";
Output
"UTF-8"

Example 2: an integer or a mode name

The substitute_character setting shows why the return type is a union. When it is a code point you get an integer; when it is a special mode you get the mode name.

Example
<?php
mb_substitute_character(63);
$char = mb_get_info('substitute_character');
mb_substitute_character('none');
$mode = mb_get_info('substitute_character');
echo json_encode(['char' => $char, 'mode' => $mode]), "\n";
Output
{"char":63,"mode":"none"}

Example 3: an array setting and the full report

The detection order comes back as an array. The full report also contains every documented key, so you can look up settings by name.

Example
<?php
mb_detect_order(['ASCII', 'UTF-8']);
$order = mb_get_info('detect_order');
$all = mb_get_info();
echo json_encode([
    'detect_order' => $order,
    'has_language' => array_key_exists('language', $all),
    'all_is_array' => is_array($all),
]), "\n";
Output
{"detect_order":["ASCII","UTF-8"],"has_language":true,"all_is_array":true}

Edge cases

  • Calling it with no argument is the same as passing "all".
  • Settings like http_input and mail_charset depend on how PHP is configured and on the request context, so their values can differ between a web server and the command line. Print them for diagnostics, but do not hard-code expectations about them.
  • Output that is derived from php.ini defaults can differ between environments. Set the value yourself first if your code or tests need a known state.
  • The detection order in the "all" array is itself an array, so the report is nested.

Common mistakes

  1. Treating it as a setter. mb_get_info('internal_encoding') never changes the encoding. Use mb_internal_encoding() for that.
  2. Assuming a string result. Passing the result of substitute_character straight into a string function ignores that it may be an integer.
  3. Using misspelled or retired type names. Stick to the documented names. The func_overload types are gone as of PHP 8.0.0.
  4. Relying on environment-specific values. Values such as the language or the HTTP encodings come from configuration, so tests that compare them to constants are fragile.
  5. Forgetting the failure value. The manual documents false on failure, so code that must be robust should check the result before use.
  • mb_internal_encoding() gets or sets the internal encoding.
  • mb_detect_order() gets or sets the detection order.
  • mb_substitute_character() gets or sets the replacement for invalid characters.
  • mb_http_output() gets or sets the HTTP output encoding.
  • mb_regex_encoding() gets or sets the encoding for multibyte regular expressions.

Recap

mb_get_info() reads mbstring's current settings, either all of them in an array or one by name. The return type depends on the setting: string, integer, array or false. Use the dedicated setters to change values, set them explicitly when you need predictable results, and check the return type before relying on it.

Tested examples

Read the internal encoding after setting it
<?php
mb_internal_encoding('UTF-8');
echo json_encode(mb_get_info('internal_encoding')), "\n";
Output
"UTF-8"
Substitute character as integer or mode name
<?php
mb_substitute_character(63);
$char = mb_get_info('substitute_character');
mb_substitute_character('none');
$mode = mb_get_info('substitute_character');
echo json_encode(['char' => $char, 'mode' => $mode]), "\n";
Output
{"char":63,"mode":"none"}
Detection order and the full report
<?php
mb_detect_order(['ASCII', 'UTF-8']);
$order = mb_get_info('detect_order');
$all = mb_get_info();
echo json_encode([
    'detect_order' => $order,
    'has_language' => array_key_exists('language', $all),
    'all_is_array' => is_array($all),
]), "\n";
Output
{"detect_order":["ASCII","UTF-8"],"has_language":true,"all_is_array":true}