Removing "?>"

Started by KensonPlays, November 12, 2011, 02:36:04 AM

Previous topic - Next topic

danielwmoore

Quote from: Kcmartz on November 14, 2011, 02:05:59 AM
So, Daniel, what you are saying is that you should only include the "?>" when you are adding HTML at the end? Otherwise keep the "?>" out? I think that it's good practice because you're less likely to have errors (like I've experienced with some mods in SMF; where when it's installed, I'll see a string of characters at the very top of the page before SMF kicks in, and looks ugly).

Yes.  Although, I can't really see a case where you would adding HTML at the end unless you are using the Model-View-Controller development pattern (MVC), which SMF does not conform to (whatever the claims are at Simple Machines) as they do not use true "View" files.  This is really not the topic for discussing whether or not SMF uses a pure MVC, so I'll get back to point.  A "View" file, is basically HTML.  It starts as HTML and may embed PHP in a number of places, especially where output depends on a condition.  You will always escape back out of PHP in a View file because you will always close with your HTML.  No PHP Processing should be required after the </HTML> tag because the purpose of a view file is to display the HTML, and nothing more. 

A Controller file will call the view, and load it for display.  In the controller, you will have the actual code that runs the application.  The view displays that information once the controller has done what it needs to do. 

A simple example might be:

Controller:
<?php 
$title 
'My Page';
require(
'myview.php');

View:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $title;?></h1>
</body>
</html>

As you can imagine, language files can easily be loaded and passed to a view.  Also, the view looks like an HTML file, but with PHP extension.  This is the reason opening and closing tags were made for PHP, so that an HTML document can be quickly turned into a templating system, without having to use a separate templating system.  You can also use "if" statements for the needed conditionals, test for browser and load appropriate CSS fixes, etc. 

Have you ever noticed that JavaScript, although it can be inserted into HTML using <script>, does not use opening and closing tags to state "This is javascript?"  PHP can use the same <script> inside HTML to start PHP, however, it is cumbersome.  Therefore, servers look to output files ending in PHP as HTML, unless otherwise escaped.  You only need the opening <?php to tell the server to process it as PHP.  Otherwise, it will process it as HTML.  The wonderful power of PHP is the power to quickly insert a bit of PHP to add a conditional, constant, or variable to HTML and enhance the power of plain HTML.  On the other hand, you can create marvelous applications with PHP, such as this forum, a social network, an online shopping cart, etc. where the PHP is just pure programming code, that calls another file to display the information (view file).  The combination is quite powerful, and when you realize the true purpose of PHP and why it was developed in that fashion.

Bottom line, the way PHP is used in a view file is why the opening and closing tags were created. 

青山 素子

I'm not going to go through and adjust all the things, but two of the more obvious errors need correcting:

Quote from: danielwmoore on November 14, 2011, 06:55:36 AM
You only need the opening <?php to tell the server to process it as PHP.  Otherwise, it will process it as HTML.

The server doesn't care about the contents of the file. Unconfigured, it'll send the raw contents of any file to the requester. When you configure a server to process PHP files, you'll be focusing on the file extension, directing the server to send the contents of the file through a specific parser and then send the output of that parser to the requester.

In the case of PHP, that opening tag is specifically meant for the PHP parser (be it through mod_php or PHP as a CGI) to recognize when to work. Specifically, the tags serve as delimiters so the parser knows where to work. It won't touch the content outside the delimiters.


Quote from: danielwmoore on November 14, 2011, 06:55:36 AM
Bottom line, the way PHP is used in a view file is why the opening and closing tags were created. 

It wasn't designed with MVC in mind. I don't even think MVC was all that well-known of a pattern when PHP was developed. PHP barely gained object orientation ten years ago, long after PHP itself was created.

The tags were created so that PHP could be embedded inside HTML rather than making a huge language that had to generate all the HTML itself (Java servelets do this, for example). Similar designs were used for ColdFusion, for example.

The whole reason you can omit the ending PHP tag is because the PHP parser will stop in either of two conditions:


  • The closing PHP tag is encountered, in which the parser will scan for the next opening tag
  • The end of the file is reached, in which case the parser exits

That second condition is the whole reason the ending tag can be left out if the file ends with PHP. Due to line-ending issues and the cases of blank lines being added to important PHP files that must all be processed before browser output, the current recommendation is to omit the ending tag, which neatly avoids such an issue.
Motoko-chan
Director, Simple Machines

Note: Unless otherwise stated, my posts are not representative of any official position or opinion of Simple Machines.


Thantos

I started leaving them out of my files a few years ago.  I don't find it sloppy at all and I haven't had any problems with it not being there.

Thantos

Quote from: Norv on November 13, 2011, 03:19:23 PM
I must have seen the problems with a poor space or newline or other characters at the end of the files in SMF causing troubles in these support boards for ages (in particular for visual verification, but not only iirc), and it's actually hard to trace to the source if you don't realize what is causing it in the first place. While I sympathize with the wish to keep the practices across languages, it simply isn't possible: it's arguable for example that practices in other languages may state that you'd better end your source files with a newline too, which simply is the kind of practice that is causing the troubles with PHP.

I don't know how far back you looked but I do recall an incident where I was using gvim and it was ninja adding a newline at the end of the file and not displaying it in the editor.  That caused a bunch of problems.

Advertisement: