2

When you look at the source code of PHP generated output usually everything is on one line, it makes it very hard for front end developers to read the code and trouble shoot.

So is there a way to get PHP generated output to go from this:

<ul><li>Hello</li><li>Hola</li><li>Bonjour</li></ul>

to

<ul>
    <li>Hello</li>
    <li>Hola</li>
    <li>Bonjour</li>
</ul>

WITHOUT using /n line breaks all over the server side code which just makes that messy.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Fat Toney
  • 41
  • 3
  • 1
    Why not use Firebug (http://getfirebug.com/) instead.. It's THE tool for front-end devs, and it pretify's everything for you! – Marko Aug 22 '10 at 23:19
  • 1
    @Marko yes I and the front end dev both use firebug but when someone looks directly into the source (control U) they will still see the messy code. Most big sites I observed have nicely for mated source code and to have otherwise seems a bit unprofessional. I know it's largely an aesthetics thing but still... – Fat Toney Aug 22 '10 at 23:26
  • 2
    Hmm - have you seen Facebook? Or Google? Whitespace/linebreaks increase page size.. and why should end-users be able to see pretty HTML? A lot of editors will format the code for you, an as far as front-end goes - I just stick to Firebug. I would never waste time trying to prettify generated output. Period. – Marko Aug 22 '10 at 23:29
  • For production use, I'd be more inclined to strip whitespace than to tidy it. Though, with GZIP it probably makes little difference, so any tidy/minimize post-processing is possibly not worth the processing power (but you would need to benchmark for your own case). – Brenton Alker Aug 22 '10 at 23:57

2 Answers2

3

For more or less clean looking PHP generated code I used " signs for the echo output Strings, wich enables you to use \n and \t or other codes like that to make new Lines or Code indentation. If you need " signs in your output you need to escape them with \". this will output a " but PHP will treat it like a usual character.

I know it is a pain getting a beautiful code this way but at least it makes it readable.

Nefrin
  • 338
  • 3
  • 11
1

You could use the tidy PECL extension - see this related question for examples.

Community
  • 1
  • 1
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348