I am encountering a bug where ANSI colors refuse to display when including a file.
The following bash command works as expected (straight bash):
echo -e "\033[31m some colored text \033[0m some white text" # Bash command displays fine
The following command run from the command line also works fine (php interpreter, command direct input via cli):
php -r 'echo "\033[31m some colored text \033[0m some white text \n";' # PHP from console displays fine
Including a file does not work.
Contents of test file:
<?php
//fails
$test = '"\033[31m some colored text \033[0m some white text \n"';
echo $test;
//escaped backslashes, also fails
$test = '"\\033[31m some colored text \\033[0m some white text \n"';
echo $test;
Including the file like this from the command line:
php index.php
Or including it like this from the command line:
php -f index.php
Always produces this:
"\033[31m some colored text \033[0m some white text \n""\033[31m some colored text \033[0m some white text \n"
Neither the line breaks nor the console colors display properly. What is causing this to choke specifically when a file runs as opposed to entering the command directly, either from bash or from PHP on the command line?
Env: I am running PHP 7.0 in iTerm on OSX, but testing in the native terminal and zsh also fails.
I have already researched this issue and ruled out all of the following
- This is OSX, not windows. The known windows bug with ANSI colors is not relevant.
- This is not a problem with my console. Colors do display in both BASH and PHP executed from the command line.
- This is not a problem with the escape prefix. On my system
\e
,\033
, and\x1B
all display correctly. All of these have been attempted and none of them make any difference. - This is not a problem with unescaped slashes. The given example demonstrates both
\033[
and\\033[
failing.
Edit:
As some comments had pointed out, the above is double quoted. Single quoted also does not work, though other users indicate that it does work for them, which leads me to believe this is some kind of arcane misconfiguration issue and not a strict PHP language problem. Updated source as requested:
<?php
//fails
$test = "\033[31m some colored text \033[0m some white text \n";
echo $test;
//escaped backslashes, also fails
$test = "\\033[31m some colored text \\033[0m some white text \n";
echo $test;
Produces the same results anyways: