Should PHP Assume Opening Tag?

Starting a new PHP project in 5.4 after spending the last 2 years programming in Ruby, the thing that’s been hardest to get used to which I never questioned before going to Ruby, is the requirement for the PHP opening tag. This prompted me to raise a feature request in the php bug tracker to add a php.ini directive to make the parser assume that files begin with PHP code, or in other words, making the opening tag implicit. This means you would omit the opening <?php tag. For a file that you wanted to start as static content, such as a template, you could either put a closing tag on the first line, for example:


?>
<div>
  <!-- blah -->
</div>

Or, you could give a second parameter to require/include to tell PHP to drop out of the parser:


require('templates/default.php', true)

I’m wondering what regular PHP programmers would think about this change. Would you welcome it with open arms, or do you prefer the current behaviour, and if so, why?

Intresting point…but…
Although I see where you are getting at, I think it would cause more problems then benefits…

Originally, php was made to be included in “normal” HTML - like so

<p> see here foobar<br>
<? include(‘foobar.php’); ?>
</p>

PHP evolved in a full OO Language now - so the question is valid for that part of PHP - but it is still used a lot for little server-side scripts within html pages…
A php-page is treated like a html page by apache, that is ok that way…
if you need a class file, you can make the opening tag with ease…

Short answer - too much changes to solve a non-relevant problem…
Peronally - I started coding 10 years ago, with php - had never a problem with the opening tag - I’m sure also you will get used to it very fast…
Milions of php coders did before you - if all those had to change theire habbits, because of ruby - hmm - nahhh…
:slight_smile:

regards
Hensel

Hi Wardrop,

What is the main reason that you would like to get rid of this, while it is a small convenience, most programming automatically insert it when you launch a page with .php? Even if you are using VI or NotePad++ it is not hard to type <?php.

I don’t know if you are interested, but you can omit the ?> on pure html pages, you only have to have the opening tag.

Curious why switching from Ruby, most people are going the other way?

Regards,
Steve

I just feel that requiring an opening tag no longer makes sense. PHP has matured beyond dynamic web pages, and is used more and more for full web applications. PHP still makes the assumptions of a templating engine. It’s almost like PHP needs two modes, an application/scripting mode, and a template mode (like Ruby’s ERB). In your apache configuration, you could apply the template mode to all files of a particular extension, e.g. phpt. Within php itself, to load a template, you should have to use something other than require() or include(), such as template() or render(). A method such as template() or render() would return the output, rather than send it straight back to the browser. Then you wouldn’t need to capture the output using output buffer functions.

In application mode, mod_php could add extra smarts, such as forking the parsed PHP program for every request, or instantiate a new application object for each request, instead of re-parsing everything - this behaviour should be out of the box, you shouldn’t need 3rd party extensions to achieve this.

By the way, I’m not switching from Ruby. PHP just looked like a more suitable option for a simple file based CMS I have in mind (and no, it’s not like any other CMS currently out there), though on further investigation, PHP’s lack of a single-point of entry makes this a little more difficult (this goes back to having an application mode in mod_php).

PHP has a couple of things going for it. First, it’s a template engine out of the box. You don’t want to throw this away, as it’s why PHP become popular, and it’s still very useful. The other thing is that PHP provides traditional object-orientated constructs, such as Classes, Interfaces, Traits, etc, while still being a dynamic language. I’m sure for all the major PHP, it’s the traditional and familiar language constructs that probably make PHP so popular among more serious programmers. I say all this because people often fear that changing PHP too much will have it lose the things unique to it, but that’s not true if the changes you make are thoughtful.

Thanks for the details, for what it’s worth your rationale makes sense to me.

Interesting thoughts regarding PHP, hopes it works for you as intended.

Regards,
Steve