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://'
PHP を使用する初めてのページ
以下の内容で hello.php という名前のファイルを作成し、Web サーバーのルートディレクトリ (DOCUMENT_ROOT) に置いてください。
例1 初めての PHP スクリプト: hello.php
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
ブラウザを使用して、/hello.php で終わる Web アクセス用 URL を指定し、 このファイルにアクセスしてください。 ローカルに開発を行っている場合、この URL は http://localhost/hello.php または http://127.0.0.1/hello.php のようになります。 しかし、これは Web サーバーの設定に依存します。 全てが正しく設定されている場合、 このファイルは PHP によりパースされ、以下の出力がブラウザに送信されます。
<html> <head> <title>PHP Test</title> </head> <body> Hello World<p> </body> </html>
このプログラムは非常に簡単なので、実際には、このようなページを 作成するために PHP を使用する必要はありません。 Hello World を PHP の echo 命令により出力しているだけです。 このファイルは、実行ファイルまたは特殊なファイルとする必要がないことに注意してください。 このファイルが拡張子 ".php" を有し、このファイルが PHP に渡される必要があると設定されているため、サーバーは PHP により解釈されるファイルを見付けることができます。 このファイルは、多くの面白いことを可能にする特別なタグを利用できる、 通常の HTML ファイルと考えることができます。
この例を試しても何も出力されない場合、または、 ダウンロード用のプロンプトが表示されるか、 テキストとしてファイル全体が表示された場合、 利用しているサーバーで PHP が利用できない可能性があります。本マニュアルの インストール の章により PHP を利用できるようにするよう管理者にきいてみてください。 ローカルに開発を行っている場合も、 インストールの章を読んで設定が全て正しく行われていることを確認してください。 解決しない問題がある場合は、多くの » PHP サポート の選択肢のどれかを利用してみてください。
この例の目的は、特殊な PHP タグ形式を示すことです。 この例では、<?php が PHP タグの開始を示しています。この後、PHP 命令を置き、終了タグ ?> を記述することにより、PHP モードを抜けています。 このように任意の場所で PHP モードを抜けて HTML ファイルに移ることができます。詳細は、 基本的な構文のセクションを参照ください。
注意: 改行に関する注意
HTML においては改行にはほとんど意味がありません。ただ、 HTML の見栄えをよくするためにも適宜改行を入れておくとよいでしょう。 ?> の直後の改行は、PHP によって取り除かれます。 複数の PHP ブロックを使用している場合や、 何かを出力するのかどうかがわからないファイルを include する際などに、 この挙動は非常に便利です。と同時に少々混乱するかもしれません。 強制的に改行させるには、?> の後に空白を置くか、 あるいは PHP ブロック内の最後の echo/print で明示的に改行を出力します。
注意: テキストエディタに関する注意
PHP ファイルを作成、編集、監理する際に使用できる、 多くのテキストエディタや統合開発環境 (IDE) があります。 これらのツールのリストの一部は、» PHP エディタのリスト で整理されています。あるエディタを推薦したい場合、上記のページを訪れ、 ページの監理者にそのエディタをリストに加えてくれないかとたずねてみてください。
注意: ワードプロセッサに関する注意
StarOffice Writer, Microsoft Word および Abiword のようなワードプロセッサは、PHP ファイルの編集には向いていません。 これらのワープロ上でテストスクリプトを編集する場合は、 ファイルをプレーンテキストとして保存していることを確認してください。 さもないと、PHP はスクリプトを読み込んで実行できません。
注意: Windows のメモ帳に関する注意
Windows のメモ帳を使用して PHP スクリプトを書く場合には、ファイルに 拡張子 .php を付けて保存したかどうかを確認する必要があります (メモ帳は、以下の防止策のどらかを適用しない限り、拡張子 .txt を自動的に付加します)。ファイルを保存する際に、 ファイル名を入力するプロンプトでファイル名を引用符で括ります (すなわち、"hello.php" とします)。 もしくは、保存ダイアログボックスにおいてドロップダウンメニュー "テキスト文書"をクリックし、"すべてのファイル" に設定を変更します。 これにより、引用符を付けずにファイル名を入力することができます。
さて、動作する簡単な PHP スクリプトを作成することができましたので、 最も有名な PHP スクリプトを作成してみましょう! phpinfo() 関数をコールすることにより、 定義済み変数、 ロードされている PHP モジュール、設定 等のシステムに関する多くの有用な情報を得ることができます。 この重要な情報を見てみてください。
例2 PHP からシステムに関する情報を取得する
<?php phpinfo(); ?>
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++)
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()).
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).
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!
document_root variable is located in your web server configuration file
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.
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.
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.
