[php] How to make php display \t \n as tab and new line instead of characters

I'm trying to output the data in my php file as plain text and I want the output to have a table format like this:

data1    data2    data3
1        a        b
2        c        d
....

When i try this:

foreach(...)
echo $data1 . '\t' . $data2 . '\t' . $data3 . '\n';

it prints them as data1\tdata2\tdata3\n...

How can I get php to read \t and \n as tab and new line?

This question is related to php character-encoding

The answer is


Put it in double quotes:

echo "\t";

Single quotes do not expand escaped characters.

Use the documentation when in doubt.


"\n" = new line
'\n' = \n
"\t" = tab
'\t' = \t

You can use HTML,

foreach(...)
echo $data1 . '&nbsp' . $data2 . '&nbsp' . $data3 . '<br/>';

"\t" not '\t', php doesnt escape in single quotes