大网数据广告活动
PHP 语言与运行手册

sapi_windows_set_ctrl_handler · Set or remove a CTRL event handler

· 52CE 我爱测

sapi_windows_set_ctrl_handler — Set or remove a CTRL event handler

sapi_windows_set_ctrl_handler

(PHP 7 >= 7.4.0, PHP 8)

sapi_windows_set_ctrl_handler — Set or remove a CTRL event handler

Description

function sapi_windows_set_ctrl_handler(?callable $handler, 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.

Parameters

handler

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:

function handler(int $event): void
event

The CTRL event which has been received; either PHP_WINDOWS_EVENT_CTRL_C or PHP_WINDOWS_EVENT_CTRL_BREAK.

Setting a null handler causes the process to ignore CTRL +C events, but not CTRL +BREAK events. add

If true, the handler is set. If false, the handler is removed.

Return Values

Returns true on success or false on failure.

Examples

Example #1 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
?>

See Also

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。