Why PHP file should OMIT the closing PHP tag?

HYEONG HWAN, MUN/ 10월 24, 2014/ 미분류/ 0 comments

https://blog.lael.be/post/396

PHP 태그를 안닫는 이유

PHP Closing Tag

The PHP closing tag on a PHP document ?> is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced by the developer, user, or an FTP application, can cause unwanted output, PHP errors, or if the latter are suppressed, blank pages. For this reason, all PHP files should OMIT the closing PHP tag, and instead use a comment block to mark the end of file and it’s location relative to the application root. This allows you to still identify a file as being complete and not truncated.

<?php echo "Here's my code!"; ?>
<?php echo "Here's my code!";
/* End of file myfile.php */
/* Location: ./system/modules/mymodule/myfile.php */

** 번역하여 요약 **
파일 끝에 스페이스나 엔터가 들어가면 php파일의 output이 생깁니다.
output이 생기면 웹서버에서 헤더를 씌우고 출력을 내보냅니다.

요즘 프로그래밍 방식인 MVC 프로그래밍에서는 (모델, 뷰, 컨트롤러, 핼퍼, 라이브러리 등등)  많은 것을 로드하고 사용해야 합니다.
그런데 실수로 파일에 스페이스바(공백)가 들어가버리면
망하므로(header already sent)

인클루드 될 모듈파일은 되도록 php closing tag는 안쓰는게 좋습니다.

즉 화면에 아무런 출력이 필요없는 파일이면 (곧 소스 전체가 php이면) close tag 를 생략하는 것이 좋습니다.
(아 물론 일반 코딩에서는 닫으셔야 합니다.)

<html>
<body>
<?php echo 'hello world!'; ?>
myday
</body>
</html>

이런 코드를 짜신다면 당연히 닫아야 겠죠?

Leave a Comment

작성하신 댓글은 관리자의 수동 승인 후 게시됩니다.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*