Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Kieran Barker
Kieran Barker
15,028 Points

What's the standard for indenting code inside PHP tags?

Whenever I use a text editor to work on PHP code, it doesn't auto-indent the code inside PHP tags. I see that Alena Holligan doesn't bother indenting, either. For example:

<?php
$fruits = array('Apples', 'Pears', 'Bananas'); // Code not indented
?>

However, I see people indenting the code as well (and I think this looks neater), like so:

<?php
  $fruits = array('Apples', 'Pears', 'Bananas'); // Code indented
?>

What's the standard? I'm also wondering about whether or not to leave out the closing PHP tag. Which is more common? Thanks in advance!

1 Answer

As far as I'm aware, the indentation is more preference than standard. It doesn't matter if you do or don't as long as you (or your team) are consistent with it (if you don't indent then make sure it's not indented in the entire project).

As for whether to include the closing tag this is what the php docs say:

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file.

Kieran Barker
Kieran Barker
15,028 Points

Thanks for the clear and concise answer, Connor! :-)