Signature
mb_get_info(string $type = "all"): array|string|int|false|nullPurpose
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_characterandstrict_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;
falseon failure, as documented. The declared return type also includesnull.
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.
mb_internal_encoding('UTF-8');
echo json_encode(mb_get_info('internal_encoding')), "\n";"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.
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";{"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.
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";{"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_inputandmail_charsetdepend 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.inidefaults 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
- Treating it as a setter.
mb_get_info('internal_encoding')never changes the encoding. Usemb_internal_encoding()for that. - Assuming a string result. Passing the result of
substitute_characterstraight into a string function ignores that it may be an integer. - Using misspelled or retired type names. Stick to the documented names. The
func_overloadtypes are gone as of PHP 8.0.0. - 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.
- Forgetting the failure value. The manual documents
falseon failure, so code that must be robust should check the result before use.
Related functions
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
mb_internal_encoding('UTF-8');
echo json_encode(mb_get_info('internal_encoding')), "\n";
"UTF-8"
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";
{"char":63,"mode":"none"}
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";
{"detect_order":["ASCII","UTF-8"],"has_language":true,"all_is_array":true}