Generating Command Line Colors with PHP
January 10, 2012


Generating ANSI (or ASCII) command line colors, or terminal colors, or whatever you call them, is easy with PHP. As I have said in my previous post, ANSI Command Line Colors under Windows, having colored text in the command line is a great help for spotting error or success messages. So here’s a quick and dirty function in PHP to do just that.

The PHP Function


function colorize($text, $status) {
 $out = "";
 switch($status) {
  case "SUCCESS":
   $out = "[42m"; //Green background
  case "FAILURE":
   $out = "[41m"; //Red background
  case "WARNING":
   $out = "[43m"; //Yellow background
  case "NOTE":
   $out = "[44m"; //Blue background
   throw new Exception("Invalid status: " . $status);
 return chr(27) . "$out" . "$text" . chr(27) . "[0m";

echo colorize("Your command was successfully executed...", "SUCCESS");


Development PHP CLI

Share this post

Written by
Mario Awad

Founder of SOFTKUBE, lead developer, and getting things done addict. Passionate about open source, user interface design, business development, and the tech world.

More about Mario Awad


A small team of experts developing simple, usable, and high-quality web solutions. We blog about business, entrepreneurship, web development, and technology.

More about us

Recent Posts

A Couple of Thought Provoking Concepts by Neil deGrasse Tyson

Discovering and Working with Template Suggestions in Drupal

How to Subscribe to Top Posts of your Favorite Subreddits via RSS

Shared Office Space for Rent in Dekwaneh, Beirut, Lebanon as a Professional Work Environment

View all posts

All Posts Categories

Business CLI Development Downloads Drupal Email Google Apps Multilingualism Open Source Philosophy PHP Quotes Science Security SEO Technology Thoughts Windows Zend Framework