Reputation: 4346
I have to use an old version of PHP while updating a production application to a modern version. My Windows 10 development machine has PHP 7.3 on it, but the old application needs PHP 5.5.
My testing framework outputs in colour which is really helpful for quickly detecting if any tests fail. However, with PHP 5.5, the output messages are interleaved with the ANSI code making them very hard to read.
PHP 7.3 can render text to the console using ANSI colours, but the same code in PHP 5.5 produces the ANSI sequences in the output instead of setting the colours. If I use ANSICON instead of the built-in command prompt, both PHP 5.5 & PHP 7.3 can output in colour.
Why is the only combination that fails to display in colour PHP 5.5 + the Command Prompt?
C:\>php55 -r "echo """\x1B[36mCyan\x1B[0m""";"
[36mCyan[0m
C:\>php73 -r "echo """\x1B[36mCyan\x1B[0m""";"
Cyan
C:\>php55 -r "echo """\x1B[36mCyan\x1B[0m""";"
Cyan
C:\>php73 -r "echo """\x1B[36mCyan\x1B[0m""";"
Cyan
Upvotes: 1
Views: 127
Reputation: 97928
The Windows console is historically very different from terminals on Unix-based OSes, as discussed in this interesting series of blogposts.
On Unix-like systems, command-line programs are not generally aware of the terminal or console they're attached to, they just receive input and write output on special file descriptors. To position the cursor, or render effects such as coloured text, they write special sequences which they hope the terminal will interpret correctly.
On Windows, the program is expected to be aware of the console itself, and use a special API for tasks such as positioning the cursor and rendering text effects.
However, recent versions of Windows can interpret special sequences based on the common "VT100" control set, but will only do so if an API call is issued to enable the feature. See documentation on the feature. This API call was added to Windows builds of PHP during the 7.2 development cycle, so older versions will not turn on the special handling.
Alternative console / terminal applications are available for Windows which work more like their Unix counterparts by default, so PHP doesn't need to do anything special to enable escape sequence processing.
Upvotes: 1