PHP sapi_windows_vt100_support() Function
The PHP sapi_windows_vt100_support() function is used to get or set VT100 support for the specified stream associated to an output buffer of a Windows console.
If enable is null, the function returns true if the specified stream has VT100 control codes enabled, false otherwise.
If enable is a bool, the function will try to enable or disable the VT100 features of the specified stream. If the feature has been successfully enabled (or disabled), the function will return true, or false otherwise.
At startup, PHP tries to enable the VT100 feature of the STDOUT/STDERR streams. Note that if those streams are redirected to a file, the VT100 features may not be enabled.
If VT100 support is enabled, it is possible to use control sequences as they are known from the VT100 terminal. They allow the modification of the terminal's output. On Windows these sequences are called Console Virtual Terminal Sequences.
Syntax
sapi_windows_vt100_support(stream, enable)
Parameters
stream |
Required. Specify the stream on which the function will operate. |
enable |
Optional. If set to true, the VT100 feature will be enabled else disabled when set to false. Default is null. |
Return Value
If enable is null, returns true if the VT100 feature is enabled, false otherwise.
If enable is a bool, returns true on success or false on failure.
Example: sapi_windows_vt100_support() default state
By default, STDOUT and STDERR have the VT100 feature enabled.
php -r "var_export(sapi_windows_vt100_support(STDOUT));echo ' ';var_export(sapi_windows_vt100_support(STDERR));"
The output of the above code will be similar to:
true true
If a stream is redirected, the VT100 feature will not be enabled:
php -r "var_export(sapi_windows_vt100_support(STDOUT));echo ' ';var_export(sapi_windows_vt100_support(STDERR));" 2>NUL
The output of the above code will be similar to:
true false
Example: sapi_windows_vt100_support() changing state
It is not possible to enable the VT100 feature of STDOUT or STDERR if the stream is redirected.
php -r "var_export(sapi_windows_vt100_support(STDOUT, true));echo ' ';var_export(sapi_windows_vt100_support(STDERR, true));" 2>NUL
The output of the above code will be similar to:
true false
Example: usage of VT100 support enabled example
Consider the example below which shows the usage of VT100 support enabled.
<?php $out = fopen('php://stdout','w'); fwrite($out, 'Forgot a lettr.'); //moving the cursor two characters backwards fwrite($out, "\033[2D"); //inserting one blank, shifting existing //text to the right -> Forgot a lett r. fwrite($out, "\033[1@"); fwrite($out, 'e'); ?>
The output of the above code will be:
Forgot a letter.
❮ PHP Miscellaneous Reference