[php-src] Issue #20727: Default mbstring detect order does not work in header_register_callback()

[email protected] (nschilling)
Newsgroups php.bugs
Message-ID <K0HM5tpB0ploZSBk6IwaWbmCq4zb095shgMvDRnd2n0@main.internal.php.net>
Issue: https://github.com/php/php-src/issues/20727
Author: nschilling

### Description

The following code:

```php
<?php
header_register_callback( fn() => mb_detect_encoding('foo') );
```

Resulted in this output:
```
Fatal error: Uncaught ValueError: mb_detect_encoding(): Argument #2 ($encodings) must specify at least one encoding in /in/0EDPU:2
Stack trace:
#0 /in/0EDPU(2): mb_detect_encoding('foo')
#1 [internal function]: {closure:/in/0EDPU:2}()
#2 {main}
  thrown in /in/0EDPU on line 2
```

But I expected this output instead:
```
```

The error seems to be present since PHP 8.0.0
https://3v4l.org/0EDPU

The error also occurs when `mb_convert_encoding()` is called instead of `mb_detect_encoding()`.

# Workaround
Setting `mbstring.detect_order` explicitly in `php.ini` prevents the error:
```
; php.ini
mbstring.detect_order = "ASCII,UTF-8";
```

Setting the order with `mb_detect_order()` inside the callback also prevents the issue:
```
header_register_callback( function() {
    mb_detect_order(['ASCII', 'UTF-8']);
    mb_detect_encoding('foo');
} );
```

Setting the order outside of the callback or setting it with `ini_set()` did **not** prevent the error.

### PHP Version

```plain
PHP 8.4.15 (cli) (built: Dec  8 2025 22:51:21) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.15, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.15, Copyright (c), by Zend Technologies
    with Xdebug v3.5.0, Copyright (c) 2002-2025, by Derick Rethans
```

### Operating System

_No response_
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.