Re: Closing PHP tag best practice?

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

 



On Sun, 2011-07-24 at 14:55 +0200, mrfroasty wrote:

> What I know its a best practice to leave the closing tag in a script
> with one php opening tag.Mostly this applies in a script that present a
> class, I think its not correct to leave closing tags in  template kind
> of script where you have several php tags.
> 
> Gr
> Muhsin
> 
> 
> On 07/24/2011 02:33 PM, Geoff Lane wrote:
> > 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,
> >
> 
> 
> -- 
> Extra details:
> OSS:Gentoo Linux
> profile:x86
> Hardware:msi geforce 8600GT asus p5k-se
> location:/home/muhsin
> language(s):C/C++,PHP,SQL,HTML
> Typo:40WPM
> url:http://www.mzalendo.net
> url:http://www.zanbytes.com
> 
> 
> 
> 



The PHP parser will automatically "add" the closing tag when it reaches
the end of the file, so you don't have to worry about the final closing
tag. This is perfectly valid:

<html>
<head></head>
<body>
<p>some content</p>
<?php
echo '<div>copyright' . date('Y') . '</div></body></html>

Not the missing closing ?> tag and that this isn't a pure PHP script,
but it mixes in HTML too.

As for best practices, it can be if you're using this on an include
file, such as a class, where any whitespace after the closing ?> tag
would be considered output, which can lead to header issues later on
down the line.

-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk



[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