Thread Safe and Non-Thread Safe in PHP: what to choose and how not to make a mistake?

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is thread safe or non-thread safe in PHP? Let's figure it out once and for all

\n\n

If you have ever installed PHP on Windows or configured a web server, you have probably seen the mysterious abbreviations TS and NTS in distribution names. Or you have read warnings in the documentation: "Choose the thread-safe or non-thread-safe version." And then the question arises: "What is this all about? And what happens if I choose the wrong one?"

\n\n

This article is your guide to the world of PHP thread safety. We will figure out how thread safe (TS) and non-thread safe (NTS) builds differ, when each one is needed, and how to avoid the trap of extension incompatibility. Let's go!

\n\n

The gist: what is thread safety?

\n\n

Imagine PHP is a chef in a kitchen. And each request to your site is a new order. In non-thread safe mode, each order is prepared by a separate chef in their own personal kitchen. They don't interfere with each other because each has their own knives, pots, and ingredients. No shared memory — no conflicts.

\n\n

In thread safe mode, you have one chef, but they work with multiple hands (threads). All hands share one kitchen, one set of tools, and one memory. The key here is not to grab the same knife at the same time. To prevent this, you need locks and synchronization — this is thread safety.

\n\n

In simple terms: Thread Safe (TS) — PHP can work in a multi-threaded environment where one process handles multiple requests simultaneously. Non-Thread Safe (NTS) — PHP works in single-threaded mode, where each request is a separate process.

\n\n

When and why did TS and NTS versions appear?

\n\n

Historically, PHP developed as a module for Apache (mod_php). Apache can work in two modes: prefork (one process per request) and worker (one process with multiple threads). For prefork, thread safety is not needed — each request is isolated. But for worker, it is critical.

\n\n

Later, with the growing popularity of FastCGI (PHP-FPM) and Nginx, it became clear: in these scenarios, multi-threading inside PHP is not used. FastCGI launches separate processes, and each one works independently. Why drag along the heavy locking code then? This is how lightweight non-thread safe builds appeared.

\n\n

How to determine which version of PHP you have installed?

\n\n

The easiest way is to run in the console or in a PHP script:

\n\n
<?php\nphpinfo();\n?>
\n\n

Find the line Thread Safety. If it says enabled — you have the TS version. If disabled — NTS.

\n\n

The second way is to look at the output of the command php -i | findstr "Thread" (Windows) or php -i | grep Thread (Linux/macOS).

\n\n

Visual differences in distribution names

\n\n

On the official website windows.php.net, you will see names like these:

\n\n
    \n
  • php-8.3.0-Win32-vs16-x64.zip — usually this is NTS (but you need to check the description)
  • \n
  • php-8.3.0-Win32-vs16-x64-ts.zip — and this is the TS version, here -ts is explicitly indicated
  • \n
\n\n

On Linux, builds are usually not separated — a single binary is used, compiled with thread support (ZTS — Zend Thread Safety), but multi-threading is actually enabled only when using Apache worker.

\n\n

Practical scenarios: when to choose TS and when NTS?

\n\n

Scenario 1: Apache + mod_php (worker MPM)

\n\n

If you are using Apache with worker or event MPM (multi-threaded modes) — you strictly need the TS version of PHP. Otherwise — fatal errors, server crashes, and data loss. It will look something like

Blogs

Book Recommendations