Current Location: Home> Latest Articles> Analysis of differences and compatibility between mb_get_info in PHP 7.x and PHP 8.x

Analysis of differences and compatibility between mb_get_info in PHP 7.x and PHP 8.x

gitbox 2025-05-29

PHP's mbstring extension plays an extremely important role when processing multibyte strings. Among them, mb_get_info() is a commonly used function to view the configuration information of the current mbstring , such as the current encoding, detection order, etc.

As PHP transitions from the 7.x era to 8.x, mb_get_info() also undergoes some subtle but important changes. This article will detail these changes and the compatibility issues they may bring.

1. mb_get_info() in PHP 7.x

In PHP 7.x, mb_get_info() has two ways of calling:

  • Call without parameters : Returns all mbstring configuration information, and the result is an associative array.

  • Call with parameters : You can pass a string parameter, such as 'internal_encoding' , to return the corresponding configuration information.

Sample code :

 <?php
// Get all mbstring Configuration information
$info = mb_get_info();
print_r($info);

// Get特定的Configuration information
$internalEncoding = mb_get_info('internal_encoding');
echo "Internal Encoding: " . $internalEncoding;
?>

This information can help developers understand the character set settings of the current environment, especially when dealing with international applications.

2. Changes in PHP 8.x

After entering PHP 8.x, mb_get_info() makes the following adjustments:

  • Some configuration information fields are discarded .
    For example, some outdated fields in the past (such as http_input , http_output ) have been completely removed in PHP 8 because mbstring no longer processes HTTP input and output. These contents are now uniformly handed over to the PHP stream and input filters.

  • The number of fields returned is reduced .
    If your code relies on some deprecated key names, such as http_output , using mb_get_info() in PHP 8 will not return them again.

  • Type declaration strengthens .
    In PHP 8, the return value type of mb_get_info() is more stringent and does not return false or other weird results as loosely as before.

  • The error handling method has changed .
    If an invalid parameter is passed, it may have returned false quietly before, and now a TypeError or ValueError may be thrown in PHP 8.

PHP 8.x sample code :

 <?php
// Get all mbstring Configuration information
$info = mb_get_info();
foreach ($info as $key => $value) {
    echo "{$key} : {$value}\n";
}

// Get internal_encoding information
try {
    $encoding = mb_get_info('internal_encoding');
    echo "Internal Encoding: " . $encoding;
} catch (ValueError $e) {
    echo "Caught error: " . $e->getMessage();
}
?>

3. Compatibility Impact Analysis

If you plan to upgrade your code from PHP 7.x to PHP 8.x, you need to pay attention to the following compatibility issues when using mb_get_info() :

3.1 Depend on abandoned fields

If there is logic in the code to directly access the abandoned fields like this:

 <?php
$httpOutput = mb_get_info()['http_output'];
?>

Then an error will be reported directly in PHP 8, prompting for an undefined array key.

Workaround : Must be rewritten to avoid relying on these disappearing key names.

 <?php
$info = mb_get_info();
if (isset($info['http_output'])) {
    $httpOutput = $info['http_output'];
} else {
    $httpOutput = 'default'; // Or give a reasonable default value
}
?>

3.2 Error handling enhancement

If you pass the parameters to mb_get_info() at will in the code, it may just return false in PHP 7, but in PHP 8, the exception will be thrown directly. For example:

 <?php
// PHP 7.x Possible to return false
var_dump(mb_get_info('non_existing_field'));

// PHP 8.x Will throw ValueError
?>

Suggestion : Add exception capture logic to improve code robustness.

3.3 Best Practice: Compatible Writing Example

In order to ensure that a set of code can run in PHP 7 and PHP 8 at the same time, you can refer to the following writing method:

 <?php
function safe_mb_get_info(string $option = null) {
    try {
        if ($option !== null) {
            return mb_get_info($option);
        }
        return mb_get_info();
    } catch (Throwable $e) {
        // Logging,Or return the default value
        error_log("mb_get_info error: " . $e->getMessage());
        return null;
    }
}

// use
$encoding = safe_mb_get_info('internal_encoding');
echo $encoding ?? 'utf-8';
?>

4. Other tips

  • If you need to learn more about mbstring and mb_get_info() , you can visit https://gitbox.net/php/manual/en/function.mb-get-info.php .

  • It is recommended to use tools such as PHPStan or Psalm to perform static code analysis before upgrading the project to promptly discover potential API compatibility issues.

5. Summary

Overall, mb_get_info() becomes cleaner, stricter, and modern in PHP 8.x, but also brings compatibility challenges. When upgrading projects, especially systems involving multilingual and character encoding sensitive systems, these changes must be carefully checked and adapted to avoid strange character processing problems after they are launched.

Remember: the problems of coding processing are often the easiest to be ignored but the most impactful.