also I just started using HomeSite which is also a very good and simple to use editor. It compliments Dreamweaver quite well.
您的第一個 PHP 網頁
在您的網頁伺服器的根目錄 (DOCUMENT_ROOT) 下建立一個稱為 hello.php 的檔案,而下列為其內容:
Example#1 我們的第一個 PHP 程式:hello.php
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo "<p>Hello World</p>"; ?>
</body>
</html>
接下來用瀏覽器輸入網頁伺服器的 URL 加上 "/hello.php",來存取您剛剛建立的檔案。若您是在本機開發程式,那網址將會是:http://localhost/hello.php 或 http://127.0.0.1/hello.php,取決於您如何配置您的網頁伺服器。如果所有的設定都正確的話,PHP 將會解析這個檔案,並輸出下列 HTML 碼至您的瀏覽器:
<html> <head> <title>PHP Test</title> </head> <body> <p>Hello World</p> </body> </html>
其實,上述的程式簡單到根本用不著使用 PHP 來建立。它只不過是使用了 echo() 敘述來輸出 Hello World,如此而已。注意一點,這個檔案不需要是可執行的或有其他特殊要求。因為使用 ".php" 當作副檔名 (伺服器已被設定把這類檔案傳給 PHP),所以伺服器會找到這個檔案,並寫用 PHP 來解譯。可以把這個視為一個一般的 HTML 檔案,它有一些可使用的特別標籤用來做很多有趣的事。
可是,如果存取此檔案時並沒有輸出任何東西,或瀏覽器提示您下載它,又或者是看到原來的程式碼,那可能您的網頁伺服器並沒有開啟 PHP,或者沒有正確設定。這時,您可以詢問伺服器管理員按照手冊裡安裝這個章節裡的指示,來為您啟用 PHP 支援。如果您是在近端開發程式,可以閱讀安裝的章節並確認所有設定是正確的,並確認您是經由 http 來存取伺服器的檔案,如果只是從檔案系統開啟檔案,那麼就不會經過 PHP 的解析。當然,若您遇上無法解決的問題,» PHP 支援那兒有大量的資源可以幫助您。
這個例子也展示了 PHP 標籤的格式應用。我們使用 <?php 來表示 PHP 程式區塊的開始,然後用 ?> 標籤來表示結束。您可以像這樣的在一個 HTML 檔內任意進入或跳出 PHP 模式。詳細的語法及應用請參考基本 PHP 語法一章。
Note: 關於換行 儘管換行在 HTML 中的實際意義不是很大,但適當地使用換行可以使 HTML 易讀且美觀。PHP 會在輸出時自動刪除其結束符號 ?> 後的一個換行。該功能主要是針對在一個頁面中嵌入多段 PHP 代碼或者包含了無實質性輸出的 PHP 文件而設計,與此同時也造成了一些疑惑。如果在 PHP 結束符 ?> 後輸出換行的話,可以在其後加一個空格,或者在最後的一個 echo/print 語句中加入一個換行。
Note: 關於文字編輯器 市場上有許多可以用來建立、編寫或管理 PHP 檔案的文字編輯器和整合式開發環境 (IDEs)。部份的這些工具可在 » PHP 編輯器清單裡找到。若您想推薦某個編輯器,請向上述網站的負責人提出要求。一個擁有語法分色顯示 (syntax highlighting) 的編輯器對程式的編輯將提供很大的幫助。
Note: 關於文字處理器 文字處理器如 StarOffice Writer,Microsoft Word 及 Abiword 並不適合用來編輯 PHP 檔案。如果您還是想用上述文字處理器來編輯 PHP 程式,請務必將檔案儲存成純文字檔,否則 PHP 將無法讀取和執行該檔案。
Note: 關於 Windows 的記事本 如果您使用 Windows 的記事本﹝Notepad﹞,請在儲存時將副檔名改為 .php。為了防止記事本在儲存時自動加上 .txt 副檔名,您可以在儲存檔案時在檔名前後加上雙引號﹝如:"hello.php"﹞,或在存檔對話方視窗的存檔類型項選"所有的檔案",然後輸入檔名,這樣就不需要雙引號了。
您已成功的建立了一個 PHP 程式。現在該是我們建立一個最廣為人知的 PHP 腳本的時候了!在您的程式呼叫 phpinfo() 後,您將會看到許多非常有用的資訊,包括您的伺服器環境,PHP 的設定情況,如可用的預設變數,已載入的 PHP 模組及組態的設定等。您不妨花些時間去認識一下這些重要的訊息。
Example#2 使用 PHP 取得系統資訊
<?php phpinfo(); ?>
您的第一個 PHP 網頁
08-Jul-2008 11:30
24-Jun-2008 09:14
Always been happy with the facilities available in Dreamweaver (cur CS3), although the footprint on your PC can be high so notepad++ comes in second for little use of PC resources.
08-Jan-2008 08:30
The most usefull editor for me is Eclipse PDT and the debugger provided by Zend to the open source community.
Please check: http://www.zend.com/en/community/pdt
http://www.eclipse.org/pdt/
It's not only an editor, but an IDE also.
Good luck!
BN
02-Jan-2008 02:46
For text editors, my current favorites are Bluefish on Linux, and Notepad++ on Windows. I believe you can't go wrong with these (and I believe Bluefish is available in many Linux distributions in pre-packaged form). I'm not affiliated with either of these; I'm just a satisfied user.
http://bluefish.openoffice.nl/
http://notepad-plus.sourceforge.net/uk/site.htm
10-Sep-2007 06:51
Re: .txt extensions at the end of Notepad files
I am not a programmer, but I do know that when you ftp files to a server, not only is the real name of the file shown that you want to transfer, but you can change it to whatever you want on the other end. I know that can make uploading a lot of files a pain, but then, I have never had to upload that many files. I remember a long time ago having to change .htm to .html, but I have never encountered the problem of having a .txt added that I didn't want.
19-May-2007 07: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).
13-Apr-2007 01:29
I noticed that it mentioned to add a "double quote" i.e "hello.php", when saving a php file when the drop down menu says txt. It is not necessary. You can type the whole name without quotes when the drown down menu is either txt or all. UNLESS, maybe for older windows? But for newer windows, it is not necessary. It works without a double quote. Anyway, if double quote is mentioned in the manual, then window versions needs to be stated.
18-Mar-2007 06:56
People with file extension woes on Win32 really just need a better text editor. Notepad2 is easy, very like notepad and supports syntax highlighting; quite a neat little thing:
http://www.flos-freeware.ch/notepad2.html
...and it won't mangle your file extensions. Of course there are lots of others, but I use this one at work and it's OK when you have to work on Windows toasters. Of course EMACS is also available on Win32 for those who like that sort of thing but Notepad2 is a neat, minimal, easy to use app for those who are generally comfortable in the Windows environment and might be intimidated by EMACS.
06-Jan-2007 06:55
No need to go through these contortions each time to make sure the file is properly saved with the .php extension. Just register .php as a file type in Windows, and then saving as hello.php without the quotes will work fine.
09-Aug-2006 01: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!
07-Jun-2006 09:26
document_root variable is located in your web server configuration file
18-Jan-2005 09: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.
11-Aug-2004 05: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.
03-Feb-2003 06: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.
