Closing PHP tag best practice?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi All,

While watching one of Jeffrey Way's CodeIgniter tutorials, I was
gobsmacked when he noted that it was best practice not to have closing
PHP tags as closing tags might introduce whitespace issues. So I tried
syntax checking a few simple pages and, to my surprise, the syntax
checkers accepted the pages even though the closing "?>" was missing.

However, I suspect that there may be issues with this. For example,
what happens if you include or require something that has no closing
PHP tag? For example, consider two files:

File: test_inner.php ----------------8<--------------------------
<?php
  echo "This is the inner code\n";
---------------------------8<------------------------------------

File: test_outer.php ----------------8<--------------------------
<html><head></head><body>
<?php
  include ('test_inner.php');
  echo "This is the outer code\n";
?>
</body></html>
---------------------------8<------------------------------------

I thought that there might be an issue with the parser attemting to
parse the closing body and html tags as PHP, so I ran a check on one
of my development machines with the above files and there was no
error. It seems that, on my development machine at least, a PHP code
block can be closed either explicitly (with '?>') or implicitly by the
EOF! However, experience has shown me that what works on one server
might not work on all of them as there are so many configuration
options and so many versions of PHP 'in the wild'.

So, is Jeffrey correct when he says that omitting the closing PHP tag
is best practice? Also, are there any 'gotchas' to doing this?

TIA,

-- 
Geoff


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux