downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

유용한 기능들> <무엇이 필요한가?
[edit] Last updated: Sat, 07 Jan 2012

view this page in

첫번째 PHP 실행 페이지

다음에 쓰여진 내용으로 hello.php를 만들고, 웹서버의 루트 디렉토리(DOCUMENT_ROOT)에 놓습니다.

Example #1 첫번째 PHP 스크립트: hello.php

<html>
 <head>
  <title>PHP 테스트</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'?> 
 </body>
</html>

웹 브라우저를 이용해서 "/hello.php"에 접근합니다. 로컬에서 개발한다면 URL은 보통 http://localhost/hello.php이나 http://127.0.0.1/hello.php일 것이지만, 이는 웹 서버 설정에 따라 달리집니다. 모두 제대로 되었다면, 이 파일은 PHP에 의해 처리되어 다음 출력을 브라우저로 전송합니다:

<html>
 <head>
  <title>PHP 테스트</title>
 </head>
 <body>
 <p>Hello World</p> 
 </body>
</html>

이 프로그램은 매우 간단해서, 실제로 이런 페이지를 만들기 위해서 PHP를 사용할 필요는 없습니다. 이 프로그램은 Hello World를 PHP echo() 표현으로 보여주기만 할 뿐입니다. 파일에 실행 권한을 줄 필요도 없고, 어떠한 특별한 처리를 할 필요도 없습니다. 서버는 ".php" 확장을 사용하였기 때문에, 이 파일을 PHP를 통해 처리해야 한다고 인식합니다. 이를 많은 일들을 가능하게 하는 특별한 태그를 가진 일반적인 HTML 파일로 생각하십시오.

이 예제를 실행했을 때, 아무것도 출력하지 않거나, 다운로드 할 것인지 묻거나, 모든 내용이 텍스트 문서처럼 보였다면, 그 서버에서 PHP가 작동하지 않거나 제대로 설정되지 않은 것입니다. 서버 관리자에게 매뉴얼의 설치 장을 참고하여 PHP를 사용할 수 있게 해달라고 요청하십시오. http를 통하여, 서버가 출력을 제공하도록 접근하고 있는지 확인하십시오. 단순히 파일 시스템에서 읽어들인다면, PHP를 통하여 처리되지 않습니다. 문제가 계속된다면, 망설이지 말고 » PHP 지원을 이용하십시오.

예제의 초점은 특별한 PHP 태그를 보여주는 것입니다. 이 예제에서 <?php가 PHP 태그의 시작을 알립니다. 그러면 PHP 상태로 돌입하게 되고, 종료 태그인 ?>에 의해 PHP 모드를 떠나게 됩니다. HTML 파일 어디에서라도 이런 방법으로 PHP 모드로 들어가고, 나올 수 있습니다. 더 자세한 내용은 매뉴얼에서 기본 PHP 문법 섹션을 읽어보십시오.

Note: 줄바꿈에 대한 주의

HTML에서 줄바꿈은 약간의 의미를 가지지만, 줄바꿈을 넣어서 HTML을 깔끔하게 보이게 하는 건 좋은 생각입니다. ?> 바로 뒤에 따라오는 줄바꿈은 PHP가 제거합니다. 이는 많은 PHP 블록을 쓰거나, 아무것도 출력하지 않는 PHP 파일을 포함할 때 매우 유용합니다. 반면, 약간 혼동할 수도 있습니다. ?>로 닫은 뒤에 공백을 하나 둠으로써, 공백과 줄바꿈을 출력하게 하거나, PHP 블록 안에서 마지막 echo/print에 명시적으로 줄바꿈을 넣을 수 있습니다.

Note: 텍스트 편집기에 대한 참고

PHP를 만들고, 수정하고, 관리할 수 있는 많은 텍스트 편집기와 IDE가 있습니다. 이러한 도구에 대한 부분적인 목록은 » PHP 편집기 목록에서 관리하고 있습니다. 편집기를 추천하려면, 위 페이지를 방문해서 페이지 관리자에게 그 편집기를 목록에 추가해달라고 하십시오. 구문 하이라이팅이 있는 편집기는 도움이 될 것입니다.

Note: 워드 프로세서에 대한 주의

StarOffice Writer, Microsoft Word, Abiword 등의 워드 프로세서는 PHP 파일을 수정하는데에는 최적화되어 있지 않습니다. 이들을 텍스트 스크립트에 이용하기 위해서는, 꼭 plain text로 저장해야만 하며, 그렇지 않으면 PHP는 그 스크립트를 읽을 수가 없고 실행할 수 없습니다.

Note: 윈도우 메모장에 대한 주의

PHP 스크립트를 윈도우 메모장을 이용해서 작성할때는, 파일이 .php 확장자를 가지도록 주의해야합니다. (메모장은 다음과 같은 절차를 거치지 않으면 자동적으로 .txt 확장자를 붙입니다) 파일을 저장할 때, 파일 이름을 묻는 부분에서 파일 이름을 따옴표로 감싸줘야 합니다. (예: "hello.php") 또는, '저장' 창에서 '텍스트 문서' 드롭다운 메뉴를 클릭해서 "모든 파일"로 설정을 바꿀 수 있습니다. 이 경우에는 따옴표를 쓰지 않아도 됩니다.

작동하는 PHP 스크립트를 성공적으로 만들었다면, 가장 유명한 PHP 스크립트를 작성할 차례입니다! phpinfo() 함수를 호출하여, 사용 가능한 예약 정의 변수, 불러진 PHP 모듈들, 설정 등 시스템과 설정에 관련한 수많은 유용한 정보를 볼 수 있습니다. 잠깐의 시간을 들여서 중요한 정보들을 확인해보십시오.

Example #2 PHP에서 시스템 정보 얻기

<?php phpinfo(); ?>



유용한 기능들> <무엇이 필요한가?
[edit] Last updated: Sat, 07 Jan 2012
 
add a note add a note User Contributed Notes 첫번째 PHP 실행 페이지
Studio at archipelago dot co dot uk 29-Jan-2011 12:40
If, when you test the file in a browser, you see the HTML code rather than your greeting, it may be because you launched the page by dragging it to your browser. The URL should start with 'http://' NOT 'file://'
xiaomao5 at live dot com 03-Sep-2010 02:29
save your php script without the BOM when using UTF-8 or you will see "锘" in front of any output. A solution is save it without BOM(available in editplus and notepad++)
miklcct at gmail dot com 03-Jan-2010 05:56
If you save your code as UTF-8, make sure that the BOM (EF BB BF) is not present as the first 3 bytes of the file otherwise it may interfere with the code if the PHP need to be run before any output (e.g. header()).
jt at fuw dot edu dot pl 19-May-2007 04:48
Well, but PHP file ownership is important when server has safe_mode enabled - HTTP server checks it, uses it to set UID of process which executes it, or may even refuse to execute such a file - e.g. if one user is owner of main PHP file, and the main file includes another, owned by other user, this is considered to be security violation (quite reasonably).
HobbyTech 08-Aug-2006 10:12
On Windows, if file extensions can be hidden, you may not SEE that you have accidently saved a file as 'Text Documents' (and that the browser has added '.txt' to the end of your 'page.html', resulting in 'page.html.txt'.) You still see only 'page.html' even though it's really 'page.html.txt'. Also, if you try to rename it, it won't work because it's not overwriting the '.txt' part and not changing the filetype.

By the way, the hiding of file extensions is ALSO a way malicious crackers get you to click on an executable virus, fooling you into thinking it's an innocent document. You should always be able to view the extensions of all files on your system.

To view all extensions, open Windows Explorer. Click the 'Tools' menu, then 'Folder Options'. In the dialog box that appears, click the 'View' tab. In the 'Advanced Settings Box', scroll down to 'Hide extensions for known file types' and click the checkbox next to it to REMOVE THE CHECKMARK. Click the 'Apply to All Folders' button near the top of the dialog. This may or may not take a few minutes. Then click the 'OK' button to close the dialog.

Now, if something accidentally gets saved as the wrong filetype, resulting in another file extension automatically appended to the one you typed, you will see it and be able to rename it.

Of course, a badly-named file can be renamed simply by using 'Save As' and saving it as the proper filetype, but if you can't see the file extension, you may not know that is the problem. Also, renaming is easier than opening, resaving as a new filetype, and then deleting the old version!
c300501 at yahoo dot com 07-Jun-2006 06:26
document_root variable is  located in your web server configuration file
onebadscrivener at gmail dot com 17-Jan-2005 05:25
OS X users editing in TextEdit will need to make sure their TextEdit preferences are set to allow plain text files.  Under the TextEdit pull-down menu, choose PREFERENCES, then under NEW DOCUMENT ATTRIBUTES in the window that pops up, click PLAIN TEXT. 

Then, in the section of that same window called "saving," DESELECT "append .txt extension to plain text files."  This will allow you to save your files with a .php extension.

Then close the PREFERENCES window.  You're good to go.
Curtis 10-Aug-2004 02:47
Expansion on saving w/ notepad/wordpad: (tested on XP; but should work on 2000,NT, and 98)

You can associate the .php file extension w/ Windows w/o going into the registry.

Open up My Computer or MSIE in file mode. Go to folder options > File types tab. Now click new. Add the extension as PHP or php. If you can't find the PHP application in the dropdown list under advanced, just go OK, for now. At least the extension is in place.

Now, try and create a php file by using the directions from this page of the PHP tutorial (should save it with the rest of your HTML files, i.e. your DocumentRoot). If you go to view your php file listed in the directory, and you see that it's still a .txt file, right-click the icon to see if you can locate "open with." If so, you should be able to browse for the appropriate file, which should be at (may vary, depending on where you installed PHP):

C:\PHP\php.exe

Click that as the default program, and the PHP logo should appear on all your scripts, and no problems saving should occur w/ any program.

Good luck.
ryan420 at earthling dot net 03-Feb-2003 02:18
Note on permissions of php files:  You don't have to use 'chmod 0755' under UNIX or Linux; the permissions need not be set to executable.  Again, this is more like a html file than a cgi script.  The only mandatory requirement is that the web server process has read access to the php file(s).  With many Linux systems, it is popular for Apache to run under the 'apache' account.  Given that HTML and other web files, like php, are often owned by user 'root' and group 'web' (or another similar group name), acceptable permissions might be those achieved with 'chmod 664' or 'chmod 644'.  The web server process, running under the 'apache' account, will inherit read only permissions.  The 'apache' account is not root and is not a member of the 'web' group, so the "other" portion of the permissions (the last "4") applies.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites