get system cpu count in php

To get the system CPU count in PHP, we can use the shell_exec function to execute the nproc command which prints the number of processing units available to the current process.

Here's the code:

main.php
$cpuCount = shell_exec('nproc');
echo "CPU count: $cpuCount";
62 chars
3 lines

The shell_exec function executes the command passed to it as string and returns the output. In this case, we pass the nproc command which prints the CPU count to the shell and captures its output in the $cpuCount variable.

We can then simply print the CPU count using the echo statement.

related categories

gistlibby LogSnag