views:

110

answers:

6

I'm using a function to generate all output in php. Using that function I can control whether to display the code like this:

<html><header></header><body><p>Hello World!</p></body></html>

or like this

<html>
<header>
</header>
<body>
    <p>Hello World!</p>
</body>
</html>

including the indentation and all.

Is there a particular value to displaying the code indented and spaced (besides seemingly slower loading time)? I usually don't need to view the source code, since I can simply access the PHP file. During development I would most likely prefer whitespace, but when on production would it necessarily be advantageous?

Thanks!

A: 

Is Javascript or PHP generating your Html? Either way - utilize an escape character.

\n = new line

\t = tab

\r = carriage return

<?php echo "This is a test. <br> \n"; ?> 
Kris Krause
The question is "Why?" not "How?"
David Dorward
+1  A: 

I prefer to omit whitespaces, especially in production.

You can still view the code through Firebug. there is no reason to do "view source".

Note that spaces can cause some problems, because they are considered as a space.

Sagi
Yes! Doing it manually (especially between templates/includes) is a major waste of time if a tool like FireBug or equivalent can make it readable for you. (Newlines can be valuable, but managing tabbing? Not so much.)
Benjamin Oakes
Yes, that's also strong point. while adding new spaces is not that hard, and with GZIP won't make a difference, it has'nt any value without managing tabs. and this is really hard with PHP.
Sagi
+4  A: 

I'd space it out if you have the option, there's nothing wrong with white-space to make something readable, and with GZip it makes the download difference not all that major anyway. You never know when you'll have to debug a style, it'll save you time later by having it pretty now, trust me.

Nick Craver
brevity at the expense of legibility is not something I would consider good coding practice either. +1
Antony Koch
+3  A: 

All whitespace is condensed to a single space, rather than nothing, so there is a slight difference. For example:

<img src="image.jpg"><img src="image2.jpg">

Will produce slightly different results to this:

<img src="image.jpg">
<img src="image2.jpg">

So at a minimum, use a single space/newline between tags. Personally I prefer using spacing on live sites because it aids live debugging, and when using gzip the difference between space and no-space is tiny anyway.

And of course, it would also help budding new developers who might like to see "how it was done".

DisgruntledGoat
Your second example is the same output as the first example with a space between the two tags, btw.
MindStalker
A: 

If you strip out whitespace you'll rue your parsimonious nature one day when you have to View Source in Internet Explorer on some remote client machine and have to wade through a swamp of HTML tags.

John Kugelman
A: 

Whitespace will unnecessarily accumulate network bandwidth. No, GZIP won't fix it up to with 100%. I myself trim all the whitespace from the response and then pass it through GZIP. The only ones who cares about whitespace in HTML source are webdevelopers who are curious how the page source look like. They are really not worth the waste of network bandwidth --unless you're practically the only visitor ;)

BalusC