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.


INCORRECT: 

<?php echo "Here's my code!"; ?> 


CORRECT: 

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



** 한국어로 요약**

파일 끝에 스페이스나 엔터가 들어가면 php파일의 output이 생깁니다.

output이 생기면 웹서버에서 헤더를 씌우고 출력을 내보냅니다.


요즘 프로그래밍 방식인 MVC 프로그래밍에서는 (모델, 뷰, 컨트롤러, 핼퍼, 라이브러리 등등)  많은 것을 로드하고 사용해야 합니다.

그런데 실수로 파일에 스페이스바(공백)가 들어가버리면 망하므로 인클루드 될 모듈파일은 되도록 php closing tag는 안쓰는게 좋습니다.


즉 화면에 아무런 출력이 필요없는 파일이면 (곧 소스 전체가 php이면) close tag 를 생략하는 것이 좋습니다.

(아 물론 일반 코딩에서는 닫으셔야 합니다.)

<body>
<?php
echo "hello world!";
?>
</body>


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


출처: https://lael.be/171

'WebDevelop > PHP' 카테고리의 다른 글

heredoc syntax  (0) 2008.10.22
Window APM + curl 연동  (0) 2008.06.27
윈도우 APM 환경에서 mail() 함수 사용  (0) 2008.06.15

은밀하게 위대하게 6월 개봉

웹툰으로 재밌게 봤던 은밀하게 위대하게가 6월 개봉한다니... 보고 싶다~!!



iframe에 form의 target을 이용한 submit시 문제점.

웹 개발시 iframe을 이용하여 form submit을 하는 경우가 종종있다.


물론 ajax를 이용하여 처리할수 있는 방법이 있으나 iframe을 사용하여 form submit시 문제가 되는 경우가 있어서 이를 위해 포스팅 해본다.


문제가 발생되는 조건은 해당 페이지에 iframe이 두개 이상 존재하며, 하나는 물론 form을 submit시키기 위한 iframe이고 다른 하나는 특정 페이지나 내용을 노출하기 위한 iframe이라고 해보자.


a.html 페이지.
<form target="frm_test">
<input type="submit" value="submit" />
</form>
<iframe src="b.html"></iframe>
<iframe name="frm_test"></iframe>

위와 같은 내용의 페이지가 있고

b.html 페이지.
<iframe name="frm_test"></iframe>

또 위와 같은 페이지가 존재한다고 할때.


예상되는 동작은 form submit시 a.html페이지의 frm_test라는 iframe으로 submit이 이뤄질거라는 예상을 하게 된다. 이는 부모 페이지에서 자식 페이지의 iframe을 참조한다는 어떠한 스크립트 내용도 없기에 그러한 예상값이 나오게 되는것인데, 실제론 a.html 페이지에서 b.html을 로드했던 iframe으로 form submit이 이뤄지게 된다.


이때 다시 b.html을 로드하는 iframe과 a.html의 frm_test iframe의 위치를 바꾸게 되면 예상했던 동작이 이뤄지게 된다.


이를 바탕으로 예상해보면 form submit시 타겟에 지정된 framename을 배열로 처리하여 부모자식에 관계없이 현재 window에 노출된 모든 iframe을 대상으로 첫번째 name을 타겟으로 잡는것같다.


iframe을 사용하여 개발을 해야할때 이러한 문제점이 있으니 name을 unique하게 정해서 중복되지 않게 한다거나, ajax를 이용하도록 하자.

'WebDevelop' 카테고리의 다른 글

svn merge  (0) 2013.04.17
전세계 IP 대역 CSV 파일  (0) 2007.11.20
우편번호 한글+영어(2007년 1월 8일)  (0) 2007.10.12
Return top