0

When developing for the web you will inevitably end up referencing a lot of files that eventually all get included and combined into a finished product. I specifically develop a lot of Wordpress sites and, as an example, the project I'm working on right now includes a file called footer.php which simply contains this code:

<?php wp_footer(); ?>

</body>

</html>

Now I'm aware what I'm asking is absolutely non-critical but I'm wondering if there are any established best practices for things like code indentation and other formatting issues within included files. The body tag is obviously nested within the html tag, so should that indentation be kept inside footer.php? At the same time it feels kind of strange to start a file by potentially indenting the first line of code a lot of times depending on where it's included, but it might end up making a big mess if you open a lot of nested tags in a different file, don't account for the nesting in your new file but then close them all in the new file.

3

In both (X)HTML and PHP, whitespace is largely irrelevant and and indentation is only useful for human readers. The browser reading the HTML and the interpreter processing the PHP will just ignore the indentation.

This makes that the best practice is that you try to makes the codes look nice to a human reader in those files and/or outputs that will read by humans on a regular basis.
The complete page output does not have to be nicely formatted HTML, as it will be only rarely seen by a human.
Files that provide portions to the page output should be formatted in such a way that the structure of the HTML in the file is optimally understandable for the persons editing the file. It usually helps if the opening and closing tags of an element are in the same file (although the body and html tags can be exceptions).

1

I completely agree with Bart on this, considering that you will rarely if ever see your footer.php as text included in the referencing file you only need to make sure footer.php is human reader friendly. The only time footer.php is seen as text and not a reference is the complete page output in the browser page source which will not be seen by humans.

So essentially ensure each file is nicely formatted so you can easily work on it but do not stress about formatting across referenced files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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