Sometimes your code needs to know how many CPUs the machine has. Tools like php-cs-fixer and Psalm use that number to check files in parallel, and Nextcloud uses it to limit how much work runs at once. This RFC from Daniel Kesselberg adds a single function that returns it:
function num_available_processors(): ?int
Why add it
Getting this number today is awkward. On Linux and FreeBSD you can run the nproc command, but only if your app is allowed to run shell commands. On Linux you can also read /proc/cpuinfo and count the entries, though you need checks to make sure the file is readable. On Windows, you need a shell command. A built-in function would behave the same way everywhere.
How it works
The function asks the operating system for the number of logical processors. If the platform isn't supported, or the answer doesn't make sense, it returns null, and your app can fall back to a default.
There are some limits and open questions:
- It counts every logical processor on the machine and doesn't check which ones your process is allowed to use. The RFC notes that Go and Rust do check that.
- Some people suggested the function shouldn't exist at all on unsupported platforms, while others want it to throw an error when it can't get a count. The current patch returns
null. - The name is still up for debate. Other suggestions include
num_cpus,cpu_count,cpu_core_countandsys_available_processors.
What it means for existing code
Nothing breaks, unless you already have your own function with the same name. The RFC says any such function probably does the same thing, so it should be easy to replace.
Where it stands
The RFC has been under discussion since May 2025 and targets the next PHP 8.x release. The page doesn't list vote choices yet, and no vote has been held. The patch is in php-src PR #11137.