tags:

views:

601

answers:

5

Is it possible to have php not to require the begin/end tags (<?php ?>) for some of the files? The code should be interpreted as php by default.

I'm aware that I can leave out the end tag (?>).

+8  A: 

No. The interpreter needs the tags to know what to parse and what not to.

code_burgar
To add this this answer, anything outside of PHP tags is simply sent to the browser as text. PHP was originally intended as a templating language, whereby the PHP tags are interspersed with HTML, not the other way round.
DisgruntledGoat
+2  A: 

It's better to not use end tag. Begin tag is neccesary.

As MaoTseTongue mentioned, in Zend documentation there is written:

For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it´ prevents the accidental injection of trailing white space into the response.
Thinker
I've heard this before, and I'm curious as to what? Is it to do with blank space at the end of the file causing problems (e.g. sending output before headers have been sent etc).
Dominic Rodger
errr... by 'what' I meant 'why', sorry!
Dominic Rodger
Do not include the closing php tag prevents "the accidental injection of trailing white space into the response". (see the [Zend Framework Documentation][1]). [1]: http://framework.zend.com/manual/en/coding-standard.php-file-formatting.htm
MaoTseTongue
It's fine to use the end tag (and aesthetically pleasing to those who like balance in their tags). The key point is not to include any trailing whitespace (eg. carriage return, line feed, tab, space, etc.) after the closing php tag because the web server will actually send those characters to the browser. Even then, for most web applications serving html, xml, json, it's generally harmless. But it can cause problems when dynamically building binary data such as an image. Another time it gets you into trouble is setting http headers when your php code has accidentally already sent whitespace.
Asaph
+1  A: 

You can store PHP code in a file or database (without <?php ?>) and then activate it using eval(). Notice, the code will perform slower. ... An eval() example comes here:

// Data layer (array, database or whatever).
$arr = array("a","b","c");

// Code layer - hint: you can get the code from a file using file_get_contents().
$str = 'foreach($arr as $v) { echo $v . " "; }';

// Shake + Bake
eval($str);

Result: a b c

/Kristoffer :-)

Kristoffer Bohmann
No sigs please, your avatar is already signing for you.
random
+1  A: 

Likewise you could make a text file that has a .php extension, and use another, 'real' php file to load that file, such as

Fake PHP file

php_info();

Real PHP file

<?
// file_contents returns a string, which can be processed by eval()
eval(file_get_contents('/path/to/file/'.urldecode($_GET['fakefile'])));
?>

In addition, you could use some mod_rewrite trickery to make the web user feel like they are browsing the php file itself (e.g. http://example.com/fakefile.php)

.htaccess file:

RewrieEngine On
RewriteRule ^(.*)$ realfile.php?fakefile=$1 [QSA]

However, if I remember correctly, your processing will be a little slower, and there are some issues with how evaled code handles $GLOBALS, $_SERVER, $_POST, $_GET, and other vars. You will have to make a global variable to pass these super globals into your evaluated code.

For example:

<?
global $passed_post = $_POST;
// only by converting $_POST into a global variable can it be understood by eval'ed code. 
eval("global $passed_post;\n print_r($passed_post);");
?>
Tony G.
+1  A: 

If you'd like to call your PHP script from the command line, you can leave the script tags using the -r switch. Extract from man php:

   -r code        Run PHP code without using script tags ’<?..?>’

Now you can invoke your script in the following manner:

php -r "$(cat foo.php)"
Török Gábor