Echo -e Content Of File Fetched Using Foreach - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs echo -e content of file fetched using foreach Ask Question Asked 8 years, 1 month ago Modified 8 years, 1 month ago Viewed 89 times Part of PHP Collective 0

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?

Share Improve this question Follow edited Nov 28, 2016 at 16:02 Ralph asked Nov 28, 2016 at 15:26 Ralph's user avatar RalphRalph 4488 silver badges19 bronze badges 7
  • what do you have before $file = 'error.log' ? – Vasil Rashkov Commented Nov 28, 2016 at 15:32
  • @VasilShaddix The opening tag (<?php). – Ralph Commented Nov 28, 2016 at 15:35
  • you have something before PHP Parse error: syntax error, unexpected '$line' (T_VARIABLE), expecting ',' or ';' in /var/www/err.php on line 12 which the php interpreter doesn't like. – Vasil Rashkov Commented Nov 28, 2016 at 15:37
  • @VasilShaddix I do. It's echo -e $line. "\n\n";. The interpreter doesn't want -e as echo works fine. All I need is for the line breaks to show in the page when accessed via the browser. – Ralph Commented Nov 28, 2016 at 15:50
  • 1 Have you tried using PHP_EOL or < br> ? – Vasil Rashkov Commented Nov 28, 2016 at 15:53
| Show 2 more comments

1 Answer 1

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 4

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>"; Share Improve this answer Follow answered Nov 28, 2016 at 15:57 Shotgun's user avatar ShotgunShotgun 6782 gold badges11 silver badges24 bronze badges 1
  • 1 Thanks a lot, that fixed it! :) – Ralph Commented Nov 28, 2016 at 16:01
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

PHP Collective Join the discussion This question is in a collective: a subcommunity defined by tags with relevant content and experts.
  • The Overflow Blog
  • The ghost jobs haunting your career search
  • Breaking up is hard to do: Chunking in RAG applications
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries
0 php return an empty array after foreach 0 read file and get specific contents and echo them (php) 0 Echo php variable inside file retrieved from get_file_contents 1 php foreach in foreach looping 0 PHP Echo File Contents With Control 5 Foreach Loop only loops once 0 Returning the file contents 2 Getting file contents in php 0 PHP statement within echo 0 PHP : Use echo after readfile

Hot Network Questions

  • Faux Random Maze Generator
  • How may I get an unlimited array of parameters, to be checked with ifundefined?
  • About two point Taylor series expansion II
  • Why does energy stored in a capacitor increase with the square of voltage?
  • In Mad Men, does the Dr Pepper Machine from 1960 prevent people from taking more bottles than they paid for?
  • What (if any) proof need a traveler have with them with the UK ETA
  • How to Simulate the variability in Vgs(off) for a N-type JFET in AGC Circuit
  • Why would a brief power-down NOT constitute a reboot?
  • YA books about a magic bag/ satchel
  • When is de/d' only used in front of a noun, especially abstract/uncountable nouns?
  • How does tip stall severity vary between normal tapered, leading-edge tapered, and trailing-edge tapered wings with the same taper ratio?
  • Determining Which Points on the Perimeter of a Circle Fall Between Two Other Points That Are on Its Radius
  • How can Anglican clergy be suspended, without pay, for teaching what the Anglican church states to be its theological beliefs?
  • Horizontal line in algorithm2e Environment
  • What information can I obtain from power spectrum density (PSD) that I can't obtain from Fourier transform of a signal?
  • "From one who is no longer of this world." from The Chocolate Box
  • Traversal Heap Sort (No Extractions)
  • Which is the default butter in the US?
  • Spotify's repository for Debian has outdated keys
  • Is there any strong logic behind the formula for the slope and curvature loadings in Nelso Siegel model?
  • I'm looking for a science fiction book about an alien world being observed through a lens. The world is inhabited by a race of lobster-like beings
  • How to troubleshoot finding a derallieur hanger?
  • Journal requires co-authors to register with ORCID, but if I don’t want to – what are my options?
  • Is it possible to shrink back a GoPro battery?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-php

Từ khóa » E Echo Php