Are spaces ok in code between tags

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I am helping someone with some code, they have already got it all in place and all the php files have no nice layout and spacing

example of their code:

<tr>                      <td>                        <h1>Send File Report</h1>                      </td>                    </tr>   '

all that white space between and etc are actuall spaces, as if spacebar was pressed many times

It gives me eye strain trying to edit things because I didn't code the site, so I don't know where most things are.
But my main question is:
Does it matter? other than it being easier when editing, do all those spaces make any difference?

should html and php files etc be layed out neatly as they look when you view the page source in a browser?

Cheers!

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

For the most part, whitespace (spaces, tabs) will not affect the layout. However there are some places where it can make a difference.

Say you have a table, and the only thing you want in the a cell is an image, sometimes the following code can make the brower act like there is a character (space) in front of the image:

and you would need to write it as

But spaces between things like the shouldn't cause any problem.

-Greg

greg's picture

He has: 1,581 posts

Joined: Nov 2005

thanks!

the spaces aren't the worst of it...
after the space issue, there are NO spaces, so everybit of code, tags, BR's, HR's, tables, td's, php inlcudes, hrefs, etc etc are all bunched up after each other line after line
there are NO return carriages at all, all the pages are just one continuous block of text Roll eyes

thanks for the speedy reply though!

Cheers!

Busy's picture

He has: 6,151 posts

Joined: May 2001

Spaces do add to the file size (minimal, but does happen)

Tidy code is always easier to add/edit

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

IMO when possible your hand written code should be properly formatted. Note, by properly formatted I mean a consistent method throughout the file (and project), not which way to format. For example there are a few ways to format things like the bracketing in if/for/while statements. There have been a few other debates here which one people prefer. IMO, which you choose doesn't matter, the important thing is to stay consistent through out your project. Indentation is a big key to easier reading.

One of the first things I do when helping someone with code they posted here, if it is more than say 8-10 lines, is copy and paste it into Zend Studio and then format it properly so it is easier to read.

When looking at source code in a browsers, the only thing that I feel should be excused from being formatted is program generated content. Take the following example, assuming the page is already started and somewhere a query has already been executed and saved to $result.

<?php
...
  <
table>
    <
tr>
      <
td>First Name</td>
      <
td>Last Name</td>
      <
td>Age</td>
    </
tr>
   
      while (
$row = mysql_fetch_assoc($result))
      {
        echo \
"<tr>\n\";
        echo \"<td>
{$row['firstname']}</td>\n\";
        echo \"<td>
{$row['lastname']}</td>\n\";
        echo \"<td>
{$row['age']}</td>\n\";
        echo \"</tr>\n\n\";
      }
   
  </table>
...
?>
As you can see, the php code is nicely formatted with indentation, but in the source code for the page, all the table rows will not be indented. However, notice that at the end the outputted data, I have a double newline (\n), this way when looking at the source code for the page, it can help seeing where the loop data was.

ie:

<?php
...
  <
table>
    <
tr>
      <
td>First Name</td>
      <
td>Last Name</td>
      <
td>E-Mail</td>
    </
tr>
<
tr>
<
td>Dave</td>
<
td>Smith</td>
<
td>23</td>
</
tr>

<
tr>
<
td>Todd</td>
<
td>Jones</td>
<
td>19</td>
</
tr>

<
tr>
<
td>Randy</td>
<
td>Davis</td>
<
td>30</td>
</
tr>

  </
table>
...
?>
As you can see, even though the generated code isn't following indentation, it is still easy to read and pick out where the looping is. Compare to code with no indentation at all and no newlines for the outputted content:

<?php
<table>
<
tr>
<
td>First Name</td>
<
td>Last Name</td>
<
td>E-Mail</td>
</
tr>
<
tr><td>Dave</td><td>Smith</td><td>23</td></tr><tr><td>Todd</td><td>Jones</td><td>19</td></tr><tr><td>Randy</td><td>Davis</td><td>30</td></tr>
</
table>
?>
Again, proper formatting IMO is does not follow one specific coding method, but is one that is clear, easy to read, and constant across a project. (Also the same goes for naming of variables too).

Also one thing to look for in helping to save file size, set your editor to use actual TABS instead of converting tabs to spaces. If you have a line indented 3 levels, that is 3 tab characters, but if you use spaces that can double that or more, depending on your preferences for how much to indent (I use 2).

The more you program, the more you will get used to what you would like formatted, not only with indentations, but also with vertical spacing. In bigger projects that can help a lot.

-Greg

He has: 1,380 posts

Joined: Feb 2002

As Greg said, the standard is tabs. For almost all languages.

Granted the spacing is varied depending on the 'standard' you listen to, what language, and what editor you use. I personally use 1 tab per level...as it seems (again) Greg does as well.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

many thanks for the tips, especially the tabs vs spaces.
That was something I had no idea about (a bit obvious as well really)

Always good advice to be found on this site! which is a sigh of relief after raking through the internet at either bad advice, millions of different opinions and not knowing which is bad or good, or the usual, can't find a bloody thing

Cheers!

Busy's picture

He has: 6,151 posts

Joined: May 2001

There was an article a while back (sorry cant find it), about how tabs are actually bad.

If your editor has the option to convert tabs into spaces this is the perferred method, I use editpad pro and have it set to 4 spaces

He has: 1,380 posts

Joined: Feb 2002

Why would tabs be bad?

For compiling languages, they get taken out.
For self-compiling languages like PHP, they get taken out at runtime.
For non-compiling languages like HTML, they're ignored.

The only real difference (for non-compiling ONLY) is whether it's a tab character or a space character, and then that all comes down to very minuscule differences in size.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.