Setting PHP Command Line Colors

Dec 14, 2011 php
This post is more than 18 months old. Since technology changes too rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with the current release.

I’m not entirely certain why this escaped me for so long, but it’s remarkably easy to set terminal colors with PHP.

Simply echo the escape/color character \033, followed by the bash color definition and your output. For reference, here is a listing of bash colors. So, for example, if we want to make something blue text, do the following:

echo "\033[34m" . 'here is blue text' . "\033[0m\n";

This simply sets the blue color, adds the blue text, and then resets the color to default, and makes a new line.

Go to All Posts