You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP:setlocale默认值来源及不同SAPI间差异问询

PHP SAPI Locale Differences: CLI vs FPM/mod_php

Great question—locale handling across PHP's different SAPIs can feel inconsistent at first, but it all boils down to how each process gets its environment variables when it starts. Let’s unpack this:

Where Default Locales Come From for Each SAPI

PHP CLI

When you run PHP from your terminal, the CLI process inherits all the environment variables from your current shell session. That includes locale-related variables like LANG, LC_CTYPE, LC_NUMERIC, etc. These variables are set either by your system’s global config (e.g., /etc/locale.conf on Linux) or your user-specific shell rc files (like ~/.bashrc or ~/.zshrc). PHP CLI doesn’t override these—it just uses whatever your shell has already set up.

PHP-FPM / mod_php

These SAPIs run as system services, not as part of a user’s interactive shell. System services start with a minimal, clean environment by default—most Linux distros don’t set locale variables for services unless you explicitly configure them. Without any LANG or LC_* variables present, PHP falls back to the C locale (the POSIX default, which is ASCII-only and doesn’t handle multibyte characters or locale-specific formatting).

Why CLI Shows C/en_US.UTF-8/C/C/C/C

The output from setlocale(LC_ALL, 0) is a comma-separated list corresponding to the six locale categories:

  1. LC_CTYPE (character classification, e.g., identifying letters vs symbols)
  2. LC_NUMERIC (number formatting)
  3. LC_TIME (date/time formatting)
  4. LC_COLLATE (string sorting)
  5. LC_MONETARY (currency formatting)
  6. LC_MESSAGES (system messages, like error text)

In your case, your shell environment is probably only setting LC_CTYPE=en_US.UTF-8 (to handle UTF-8 character encoding) while leaving all other LC_* variables unset. When a category isn’t explicitly set, it defaults to the value of LC_ALL (or LANG, if LC_ALL isn’t set)—which in your shell is C. That’s why only the first category (LC_CTYPE) shows en_US.UTF-8, and the rest stay as C.

Why PHP-FPM Uses All C Locale

Since FPM starts as a system service, it doesn’t inherit any user-specific locale settings. Without explicit configuration, none of the LANG or LC_* variables are present in its environment. PHP then defaults every locale category to C across the board.

If you want FPM to match your CLI’s locale behavior, you can add environment variables to your FPM pool config (usually www.conf):

# Set LC_CTYPE to match your CLI, leave others as C
env[LC_CTYPE] = en_US.UTF-8
# Or set all categories to UTF-8
env[LANG] = en_US.UTF-8

After making this change, restart the PHP-FPM service for it to take effect.

内容的提问来源于stack exchange,提问作者Alana Storm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:24:47