sapi_windows_set_ctrl_handler
(PHP 7 >= 7.4.0, PHP 8)
Set or remove a CTRL event handler
Description
Sets or removes a CTRL event handler, which allows Windows CLI processes to intercept or ignore CTRL+C and CTRL+BREAK events. Note that in multithreaded environments, this is only possible when called from the main thread.
Parameters
handlerA callback function to set or remove. If set, this function will be called whenever a CTRLC or CTRLBREAK event occurs. The function is supposed to have the following signature:
handler(int $event): voideventThe CTRL event which has been received; either
PHP_WINDOWS_EVENT_CTRL_CorPHP_WINDOWS_EVENT_CTRL_BREAK.
Setting a null
handlercauses the process to ignore CTRLC events, but not CTRLBREAK events.addIf true, the handler is set. If false, the handler is removed.
Return Values
Success
Examples
Basic sapi_windows_set_ctrl_handler() Usage
This example shows how to intercept CTRL events.
<?php
function ctrl_handler(int $event)
{
switch ($event) {
case PHP_WINDOWS_EVENT_CTRL_C:
echo "You have pressed CTRL+C\n";
break;
case PHP_WINDOWS_EVENT_CTRL_BREAK:
echo "You have pressed CTRL+BREAK\n";
break;
}
}
sapi_windows_set_ctrl_handler('ctrl_handler');
while (true); // infinite loop, so the handler can be triggered
?>