Current Location: Home> Function Categories> sapi_windows_set_ctrl_handler

sapi_windows_set_ctrl_handler

Set or delete a CTRL event handler
Name:sapi_windows_set_ctrl_handler
Category:Miscellaneous
Programming Language:php
One-line Description:Set up a handler for capturing control signals on Windows platforms

Function name: sapi_windows_set_ctrl_handler()

Function Description: The sapi_windows_set_ctrl_handler() function is used to set the handler for capturing control signals on Windows platforms.

Applicable version: PHP 4 >= 4.2.0, PHP 5, PHP 7

Syntax: bool sapi_windows_set_ctrl_handler ( callable $callback [, bool $add = true ] )

parameter:

  • callback: Required. Specifies the name of the callback function or method to be executed. The callback function accepts an integer parameter, indicating the received control signal.
  • add: optional. Specifies whether to add or delete control signals. The default is true, indicating the handler for adding control signals.

Return value: Return true on success, and false on failure.

Example:

 <?php function my_handler($signal) { echo "Received signal: " . $signal . "\n"; } // 添加控制信号的处理程序sapi_windows_set_ctrl_handler('my_handler'); // 模拟发送控制信号posix_kill(posix_getpid(), SIGINT); ?>

Output:

 Received signal: 2

In the above example, a callback function my_handler() is first defined, which will be called when a control signal is received. Then use the sapi_windows_set_ctrl_handler() function to set my_handler() as the handler for the control signal. Finally, a SIGINT control signal is sent by using the posix_kill() function, causing my_handler() function to be called and the corresponding signal value is output.

Similar Functions
Popular Articles