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 (?>).
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 (?>).
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)"
php -r 'echo "Hello World!\n";' always throws error PHP Parse error: syntax error, unexpected ''echo' (T_ENCAPSED_AND_WHITESPACE), expecting end of file in Command line code on line 1 ?No. The interpreter needs the tags to know what to parse and what not to.
.php extension signals to a server to call a php interpreter anyhow.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.
<!doctyp html> might be a problem. Lets file a RFCLikewise you could make a text file that has a .php extension, and use another, 'real' php file to load that file, such as
php_info();
<?
// 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)
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);");
?>
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
This is a quote from another answer on superuser.com: (https://superuser.com/questions/612178/how-to-set-php-syntax-for-a-file-without-php-tag)
Put
#<?phpat the top of the file. PHP will treat it as a comment. Sublime won't. Perfect.
If helpful for anybody I also want to point out that this trick works for vscode. I am using this bc I have some software where i have to input pure php without open tag and i also save the whole code in a php file which requires the open php tag. With this comment trick i can still get syntax highlighting and also copy the whole file in the software where it will treat the first line as a comment.
#<?php and <?php equally in every possible way. Besides, the answer you quoted is outright NONSENSE: "PHP will treat it as a comment" - PHP will NEVER treat it as a comment. So I still have no idea what are you talking about. In the end you ADDED the tag, which is contrary to what is asked here.