Ralph
Ralph

Reputation: 458

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 echo, 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?

Upvotes: 0

Views: 89

Answers (1)

Shotgun
Shotgun

Reputation: 678

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>";

Upvotes: 4

Related Questions