phpbashshellubuntucommand-line-interface

How to enable color for PHP CLI?


How do I enable the colors for output of CLI? The below one is, running on Ubuntu.

enter image description here

If you see the screenshot, obviously the colors is enabled for terminal. And, if I call echo, it doesn't colorize the result, but if I use echo -e, it colorizes.
I checked manual page of echo, and -e means enable interpretation of backslash escapes
How can I enable this option for PHP CLI?


Solution

  • First we use an escape character so we can actually define a output color. This is done with \033 (\e). Then we open the color statement with [31m. Red in this case.

    The "some colored text" will be the text outputted in a different color. And after that we have to close the color statement with \033[0m.

    php -r 'echo "\033[31m some colored text \033[0m some white text \n";'
    

    ref 1

    ref 2

    enter image description here