n3rve n3rve - 2 months ago 18
PHP Question

echo -e content of file fetched using foreach

Forgive me, I'm not a programmer.

I am trying to include the last lines of a log file on a PHP page with two line breaks after each line read. Using responses from other questions, I came up with the following:

$file = "error.log";

foreach(file($file) as $line) {
echo -e $line. "\n\n";

When I execute this on the command line using
php file.php
, it is displayed correctly with the line breaks, however, on a webpage, it ignores the line breaks and prints a dirty output.

I tried to use
echo -e
in place of
, but that doesn't even present the dirty output, but instead throws an error:

PHP Parse error: syntax error, unexpected '$line' (T_VARIABLE), expecting ',' or ';' in /var/www/err.php on line 12

I'm assuming
echo -e
isn't allowed within the foreach function? What am I doing wrong and how can I achieve the desired results?


echo in PHP is different than the echo command line in linux. You can't pass arguments to echo in PHP like that.

try :

echo nl2br($line . PHP_EOL);

Or if you only care about the browser

echo $line . "<br>";