The PHP Online Conference 2021


(PHP 7 >= 7.4.0)

sapi_windows_set_ctrl_handlerSet or remove a CTRL event handler


sapi_windows_set_ctrl_handler ( callable $callable [, bool $add = TRUE ] ) : bool

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.

Liste de paramètres


A callback function to set or remove. If set, this function will be called whenever a CTRL+C or CTRL+BREAK event occurs. The function is supposed to have the following signature:

handler ( int $event ) : void
The CTRL event which has been received; either PHP_WINDOWS_EVENT_CTRL_C or PHP_WINDOWS_EVENT_CTRL_BREAK.
Setting a NULL callable causes the process to ignore CTRL+C events, but not CTRL+BREAK events.


If TRUE, the handler is set. If FALSE, the handler is removed.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.


Exemple #1 Basic sapi_windows_set_ctrl_handler() Usage

This example shows how to intercept CTRL events.

function ctrl_handler(int $event)
    switch (
$event) {
"You have pressed CTRL+C\n";
"You have pressed CTRL+BREAK\n";

while (
true); // infinite loop, so the handler can be triggered

Voir aussi

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top