sapi_windows_set_ctrl_handler

(PHP 7 >= 7.4.0, PHP 8)

sapi_windows_set_ctrl_handlerSet or remove a CTRL event handler

Description

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:

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

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Definition of stored procedures - an introduction

Stored procedures are predefined SQL code blocks that are stored in a database and can be called up as required. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial