Tuesday, 15 May 2012

syntax - justification for removing closing tag from a PHP file -



syntax - justification for removing closing tag from a PHP file -

this question has reply here:

why 1 omit close tag? 13 answers

ok, hate doing no reason, if appears stupidest thing i've ever seen, here goes:

i'm encountering codebase php files start <?php, don't end ?>

i have not seen documentation on why, apparently has "security".

can enlighten me why break mutual sense , leave out closing php tag @ bottom of file?

the documentation states:

if file pure php code, preferable omit php closing tag @ end of file. prevents accidental whitespace or new lines beingness added after php closing tag, may cause unwanted effects because php start output buffering when there no intention programmer send output @ point in script.

it has nil "security". has functions behaviour depends on whether output has been sent client or not. best illustration function header(). meant manipulating http response headers. function work before output has been send - in http there headers cannot beingness sent after body.

let's nature of php. scripting language can embedded other documents, html:

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

when embedded other documents php's output inserted document, leaving original document as-is, meaning sending it's literal content client.

when have class file, example:

<?php class foo { } ?><whitespace>... <newline> <newline>

... closing php tag , have 2 forgotten spaces , new lines in file. php send spaces , new lines client, meaning function header() wouldn't work anymore. text document embedded php code. (unlike source code files in other languages). php replace part between <?php ?> , send results + remaining parts of file client.

if omit closing php tag in case, php parser ignore spaces , newlines because don't contain code.

php syntax

No comments:

Post a Comment