mb_internal_encoding

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

Sets/gets the process-wide default multibyte encoding - the implicit value behind every mb_* call that omits its encoding argument. Global mutable state: modern style prefers passing it explicitly.

C C23

C23
#include <locale.h>
#include <stdio.h>
#include <string.h>

int main(void) {
    /* C has no text-encoding knob: char is just bytes, and setlocale only
       switches collation/message tables - the "C" locale is byte-oriented */
    setlocale(LC_ALL, "C");
    printf("%s\n", setlocale(LC_ALL, NULL)); /* "C": the locale's name */
    printf("%zu\n", strlen("你好")); /* 6: bytes - the C view of "characters" */
    return 0;
}
Output
C
6

C++ C++26

C++26
#include <cstring>
#include <iostream>
#include <locale>

int main() {
    // the default global locale is classic "C": encoding is not a C++ concept,
    // strings are byte sequences and only locales dress them up
    std::cout << std::locale().name() << "\n"; // "C"
    std::cout << std::strlen("你好") << "\n"; // 6 bytes
    return 0;
}
Output
C
6

C# v11.0.100

v11.0.100
// .NET has no runtime-wide encoding switch either: Encoding.Default
// is a fixed default (UTF-8 in modern .NET), not a knob to turn
Console.WriteLine(System.Text.Encoding.Default.WebName); // utf-8
Console.WriteLine("你好".Length); // 2: string is UTF-16, encoding lives at the byte boundary
Output
utf-8
2

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	// Go strings are ALWAYS UTF-8: there is no global encoding to set.
	// The language fixed the choice instead of adding a knob
	fmt.Println(utf8.RuneCountInString("你好")) // 2
	fmt.Println(utf8.ValidString("你好"))      // true
}
Output
2
true

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // the JVM's global knob is file.encoding - a STARTUP option (-D),
        // not a settable runtime call; UTF-8 by default since Java 18 (JEP 400)
        System.out.println(java.nio.charset.Charset.defaultCharset().name());
        System.out.println("你好".length()); // 2: String is always UTF-16 inside
    }
}
Output
UTF-8
2

JavaScript v26.13.1

v26.13.1
// No global text encoding exists to set: JS strings are UTF-16 code
// units, period. Encodings appear only at the byte boundary
console.log("你好".length); // 2: encoding is a non-concept for string itself
console.log(Buffer.from("你好", "utf-8").length); // 6: bytes only here
console.log(Buffer.from("你好", "utf-16le").length); // 4: same string, other bytes
Output
2
6
4

PHP v8.6.0

v8.6.0
// get: the process-wide default every mb_* call inherits
var_dump(mb_internal_encoding()); // UTF-8
// set: switches the default for the WHOLE process - global mutable state
mb_internal_encoding('SJIS');
var_dump(mb_internal_encoding());
mb_internal_encoding('UTF-8'); // restore
var_dump(mb_internal_encoding());
var_dump(mb_strlen('你好')); // 2: now measured under UTF-8 again
Output
string(5) "UTF-8"
string(4) "SJIS"
string(5) "UTF-8"
int(2)

Python v3.15.2

v3.15.2
import sys

# sys.getdefaultencoding is a read-only fact about str, not a knob:
# Python 3 strings are Unicode; encodings live at I/O boundaries
print(sys.getdefaultencoding())          # utf-8
print(sys.stdin.encoding is not None)    # True: streams carry their own encoding
print("你好".encode("utf-8").decode("utf-8")) # prints the string back
Output
utf-8
True
你好

Rust v1.99.1

v1.99.1
fn main() {
    // Rust String is valid UTF-8 BY CONSTRUCTION: no global encoding knob
    // exists - invalid bytes are rejected at the boundary, not reinterpreted
    println!("{}", "你好".chars().count()); // 2
    println!("{}", std::str::from_utf8(&[0xE4, 0xBD, 0xA0]).is_ok()); // true: valid
    println!("{}", std::str::from_utf8(&[0xFF]).is_err()); // true: rejected at the gate
}
Output
2
true
true

TypeScript v26.13.1

v26.13.1
// No global text encoding exists to set: strings are UTF-16 code units.
// Encodings appear only at the byte boundary
console.log("你好".length); // 2
console.log(Buffer.from("你好", "utf-8").length); // 6
Output
2
6

Language differences

C: no encoding knob - char is just bytes, and setlocale only swaps collation/message tables (LC_CTYPE affecting the single-character functions); the concept of a text encoding does not exist at the C level; strlen("你好") returning 6 bytes IS "characters" in C's eyes.

C++: the default global locale is classic "C" (std::locale().name() in the example); std::locale("zh_CN.UTF-8") can change the global locale, but its reach (numbers/time/collation) is far wider than encoding - no counterpart.

C#: no runtime switch - Encoding.Default is fixed at UTF-8 in modern .NET (it was the ANSI code page in .NET Framework days); the encoding choice happens at each Encoding.UTF8.GetBytes call site, not in global state.

Go: the language fixes UTF-8 at the type level - the string spec itself promises UTF-8, with no knob to set; source files, runtime strings and range iteration are all UTF-8; other encodings go through x/text/encoding at the I/O boundary.

Java: the global knob is file.encoding - a STARTUP -D option (-Dfile.encoding=UTF-16), not a settable runtime call; UTF-8 is the fixed default since Java 18 (JEP 400); Charset.defaultCharset() only reflects it; String is always UTF-16 inside.

JavaScript: no global encoding concept - a string is a sequence of UTF-16 code units, and "encoding" only appears at the byte boundary (the encoding arguments of Buffer/TextEncoder); the mb_internal_encoding pattern cannot exist here because the internal string representation is fixed by the language.

PHP: PROCESS-WIDE global mutable state - the no-arg call returns the current default (UTF-8 on CLI), and setting it changes every subsequent mb_* call that omits its encoding argument (mb_strlen/mb_strpos/mb_substr...); legal values come from mb_list_encodings (UTF-8/SJIS/EUC-JP etc.); the php.ini mbstring.internal_encoding has been deprecated since PHP 5.6 (following default_charset); modern style: pass encoding explicitly to every mb_* call and leave the global default to legacy code.

Python: sys.getdefaultencoding() is a READ-ONLY fact (always utf-8 in Python 3, describing the nature of str) - sys.setdefaultencoding was removed in Python 3; encoding choices live at I/O boundaries: open()'s encoding argument, sys.stdin.encoding, encode/decode call sites; there is no global mutable counterpart.

Rust: String/&str are valid UTF-8 BY CONSTRUCTION - str::from_utf8 gates the boundary (example), rejecting invalid bytes instead of reinterpreting them; no knob, no global state - the type system expresses "this is valid UTF-8" directly.

TypeScript: same as JavaScript.