PHP is a widely used scripting language for web development. Unlike compiled languages, PHP code is dynamically interpreted at runtime. The PHP interpreter provides the execution environment and supports two main modes: Thread Safe (TS) and Non-Thread Safe (NTS). The TS mode is designed to be safe for multi-threaded environments, while the NTS mode is intended for single-threaded setups.
Although both modes can use the same extension libraries, these extensions must match the thread safety type of the PHP interpreter. Otherwise, runtime instability or errors may occur.
Below, we will show how to identify whether your current PHP environment is TS or NTS.
On Linux or Unix-like systems, you can determine if PHP is thread safe by using the command line or inspecting the php.ini file.
Open a terminal and run the following command:
php -i | grep 'Thread Safety'
The output will be similar to:
Thread Safety => disabled
If the result is enabled, PHP is running in thread safe (TS) mode; otherwise, it is non-thread safe (NTS).
Open your php.ini file and search for Thread Safety. Typical TS mode related configuration might look like this:
; ; Thread Safety ; Default Value: enabled ; Development Value: enabled ; Production Value: enabled
If such comments appear and the default or development values are enabled, it indicates PHP is running in thread safe mode.
If PHP is not thread safe, it is running in non-thread safe mode by default. You can further confirm by running this command line script:
php -r 'echo (php_sapi_name() === "cli" && !empty(ini_get("disable_dl"))) ? "Non-thread-safe\n" : "Thread-safe\n";'
If the output is Non-thread-safe, your PHP is in NTS mode.
Knowing your PHP interpreter's thread safety mode is crucial for environment setup and extension compatibility. Using command line tools or checking php.ini, you can quickly identify if PHP is TS or NTS, preventing runtime errors caused by mismatched extensions. Always ensure your extensions correspond to your PHP thread safety mode to maintain a stable and reliable environment.