Our program demonstrates how to handle Unix signals in PHP. Here’s the full source code:
When we run this program, it will block waiting for a signal. By using Ctrl+C (which the terminal shows as ^C), we can send a SIGINT signal, causing the program to print “Caught SIGINT” and then exit.
Note that PHP’s signal handling is only available when PHP is compiled with the --enable-pcntl option, which is typically the case for CLI installations but not for web server modules.
Also, unlike the Go example which uses channels, PHP uses callback functions for signal handling. The pcntl_signal_dispatch() function is used to check for and dispatch signals in the main loop, which is necessary because PHP doesn’t handle signals asynchronously by default.
This approach allows PHP scripts to respond to Unix signals, enabling more robust command-line tools and long-running processes.