26
Sep
2011

Closing Tag in PHP

If file consists only of php code (without HTML code), then the best practice is to omit closing tag (?>).

Although closing tag rarely causes any problems (the most common - accidentally putting space character after "?>" can causes sending a HTTP headers and then if you want to send headers manually for example for the redirect purposes, it's simple doesn't work 'cause headers has already been sent), nevertheless giving up to use closing tag is a rather good practice in php.

Generally speaking, if closing tag is not mandatory, so why we need to use it?

So, in case

  1. <?php
  2. echo 'Hello, world!';
  3. ?>
  4. <html>something</html>

we must using closing tag for keeping apart php and html code.

But in this case

  1. <?php
  2. echo 'Hello, world!';

closing tag should be omitted.

And in this case

  1. <html>something</html>
  2. <?php
  3. echo 'Hello, world!';
  4. ?>

you can omit closing tag or not on your own discretion. Though, it's better not to omit "?>" so the code become more clear to understand.

Category: 
0