17

I have read that it is advised (especially with php 7) to not close the php files with ?>

Many of my WP php files end like this:

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Should I remove the closing tag and have something like this

<?php get_sidebar(); ?>
<?php get_footer(); 

at the end of my files?

2
  • 2
    If there is nothing after your not closed php tag it's completely ok, as I observed in many frameworks like Laravel, they don't close php tag at the end of the files, and by the way, the WordPress core files also followed the same pattern and not closed the php tag at end, so I think we should follow WordPress coding style and not closing them.
    – Amirmasoud
    Dec 4, 2015 at 14:54
  • 1
    Possibly related ticket on trac: #10633
    – Sven
    Dec 4, 2015 at 15:34

2 Answers 2

22

Yes, please avoid closing PHP tags at the end of the file, not only with PHP 7, but with PHP 5 as well.

Reason is that if you close the tag, anything that is after the tag, even a blank line, will be sent to output and will make PHP to send headers as well preventing cookie to be set, redirect to work, feed to be valid, and so on.

I guess that you ever encountered a message like

Cannot modify header information - headers already sent by (output started at ...) in ... on line ...

A closing ?> at end of the file can be the cause.

6
  • 2
    I believe this is the correct answer. Even Underscores, where I originally got my template from, has removed the closing tag in the latest update.
    – IXN
    Dec 4, 2015 at 19:08
  • @IXN This is correct if the entire file is PHP and therefore consists of a single PHP code block (the PHP code block is implicitly closed at the end of file). However, if you have multiple PHP code blocks and are mixing HTML and PHP, as implied by the example in your question, then you should explicitly close the PHP code block, as TheDeadMedic states in his answer.
    – MrWhite
    Dec 7, 2015 at 11:02
  • I think the only exception is if the last php code block is followed by html. But if the last symbols on the file are ?> they can/should be omitted. Or so I understand.
    – IXN
    Dec 7, 2015 at 11:09
  • The main reason for omitting (or including) the closing PHP tag is to avoid introducing silly bugs during development. As mentioned above, for a wholly PHP file, this can avoid accidental output resulting in error. For a file that is already mixing HTML output and PHP this cannot result in error. However, the omission of the closing PHP tag in this second case has potential to introduce bugs, since you now have to remember to explicitly close the PHP tag before adding trailing HTML content - this isn't something you do in wholly PHP code file.
    – MrWhite
    Dec 7, 2015 at 12:04
  • If you look through the WP source, you will see that (most) of the PHP includes omit the trailing PHP tag. However, the template files (that mix HTML and PHP code blocks) always include the closing PHP tag.
    – MrWhite
    Dec 7, 2015 at 12:06
13

Given your specific example, I would keep the closing tag i.e. one-line function calls within a template. It's consistent and aids clarity (in the same way WordPress recommend trailing commas for arrays)- otherwise imagine if a non-developer picked up your file and started adding to it:

<?php get_footer();

<div>What the hell am I doing wrong?</div>

However, for all other files (functions, includes etc.), the advice is most definitely a good idea:

<?php // Start of file

class MY_Class {
    function just_do_it() {
    }
}

// Bye bye closing tag

I find it's cleaner, and as other's have mentioned, no risk of the dreaded "headers already sent".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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