How to enable color for PHP CLI?
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
For lazier
function colorLog($str, $type = 'i'){
switch ($type) {
case 'e': //error
echo "\033[31m$str \033[0m\n";
break;
case 's': //success
echo "\033[32m$str \033[0m\n";
break;
case 'w': //warning
echo "\033[33m$str \033[0m\n";
break;
case 'i': //info
echo "\033[36m$str \033[0m\n";
break;
default:
# code...
break;
}
}