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

search for in the

フォームの処理> <PHP を使用する初めてのページ
Last updated: Fri, 14 Nov 2008

view this page in

実用的な例

次に、より実用的なことをしてみましょう。 ページを見ているユーザが使用しているブラウザの種類を確認してみます。 これを行なうには、ブラウザが HTTP リクエストの一部として送信した user agent 文字列を調べます。 この情報は、変数 に保存されています。PHP では、変数名は常にドル記号で始まります。 ここで使用する変数は、$_SERVER['HTTP_USER_AGENT'] です。

注意: $_SERVER は、 Web サーバ関連情報を全て保持する PHP の特別な予約変数です。詳細は、 スーパーグローバル を参照してください。 これらの特別な変数は、» 4.1.0 で導入されました。これ以前は、 $HTTP_SERVER_VARS のような古い配列 $HTTP_*_VARS を代わりに使用していました。 古いとはいえ、これらの変数はまだ存在しています (古いコードに関する注記も 参照してください)。

この変数を表示するには、以下のようにします。

例1 変数を出力する (配列要素)

<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>

このスクリプトの出力例は以下のようになります。

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
     

PHP で利用可能な変数の には多くの種類があります。上の例では、 配列 の要素を出力しています。配列は、非常に有用です。

$_SERVER は、PHP で自動的に利用可能な変数のひとつに過ぎません。マニュアルの 定義済の変数 のセクションでリストを参照することができます。 あるいは、完全なリストを取得するには、さきほどのセクションで使用した phpinfo() 関数の出力を確認します。

PHP タグの中に複数の PHP 命令を置くことができ、echo 文以上のことを行なうコードブロックを作成することができます。 例えば、インターネット・エクスプローラかどうかを調べたい場合は、 以下のようにします。

例2 制御構造 および 関数の使用例

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
  echo 
'あなたはInternet Explorerを使用しています<br />';
}
?>

このスクリプトの出力例は以下のようになります。

あなたはInternet Explorerを使用しています<br />

ここで、新しい概念をいくつか導入します。 if 文を使用しています。 C 言語の基本構文を知っているとしたら、理解できると思います。 C 言語や上記の構文を使用する他の言語をあまり知らない場合には、 PHP の入門書を手にとって最初の数章を読むか、このマニュアルの 言語リファレンスの部分を読むべきです。

二番目の新しい概念は、strpos() 関数のコールです。 strpos() は PHP に組み込まれた関数で、 文字列の中である文字列を探します。この場合、 $_SERVER['HTTP_USER_AGENT'] (いわゆる干し草の山 【haystack】) の中で "MSIE" (いわゆる針【needle】) を探しています。 この文字列が見つかった場合、 この関数はこの関数は文字列の相対的な位置を返し、 見つからなかった場合には FALSE を返します。 この関数が FALSE を返さなければ、 if 文は TRUE と評価し、その{波括弧}の中のコードが実行されます。 そうでない場合は、実行されません。 if, elsestrtoupper()strlen() のような他の関数で、似たような例を作ってみてください。 関連するマニュアルの各ページにも例がのっています。 関数の使用法に自信がない場合には、マニュアルの 関数定義の読み方および PHP関数のセクションの両方を 読んでみると良いでしょう。

この例を少し発展させて、PHP ブロックの中からでも PHP モードから出たり入ったりすることができることを以下に示します。

例3 HTML および PHP モードの両方を混在させる

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
?>
<h3>strposが非falseを返しました</h3>
<center><b>あなたはInternet Explorerを使用しています</b></center>
<?php
} else {
?>
<h3>strposがfalseを返しました</h3>
<center><b>あなたはInternet Explorerを使用していません</b></center>
<?php
}
?>

この例の出力は以下のようになります。

<h3>strposが非falseを返しました</h3>
<center><b>あなたはInternet Explorerを使用しています</b></center>

何かを出力する際に PHP の echo 文を使用する代わりに、PHP モードを抜けて通常の HTML を送信しています。ここで注意すべき重要で強力な点は、 スクリプトの論理フローが損なわれないということです。 strpos()TRUE または FALSE のどちらを返すか、言い換えるとMSIE が見つかったかどうかに基づき、HTML ブロックだけが見る側に送信されることになります。



add a note add a note User Contributed Notes
実用的な例
Dave
04-Oct-2008 01:12
In my opinion this tutorial is already following the excellent approach of using small focused examples and building on them.  This makes it easier to understand and gives the reader a chance to try some simple experiments without having to wade through the muck of a complex framework.

I assume at some point you will show the type of frameworks you can use in PHP but it would be foolish to burden you reader with those until they understand the basics.
Liz
28-Sep-2008 06:08
Ducky,

I have started teaching my 14 year old daughter basics of web development. We've started off with separating document structure form presentation from logic. It's actually worked out well - and she appreciates that it will help her down the road.

Please do NOT underestimate the intelligence of your readership by assuming that such "complex things" as best practices can't be taught from the start. Teaching best practices from the start are the types of things that give students a leg up when they get out into the work force and have to ramp up in new job situations - little things like that do get noticed by employers and coworkers.
Anonymous
02-Sep-2008 08:26
To ducky:

Actually it's a good idea to start the habit of separating logic from presentation since the beginning (like when you're just starting to learn html and css). Otherwise you'll get used to not work that way, and when you want to do it it'll be much harder.
rok
17-Jun-2008 11:25
Seperating logic and presentation should be introduced at start. I'm just learning PHP but have background in Perl, C and C++ and find mixed html/code extremly complex to maintain. It would be very nice if this beginners tutorial already presented on how to seperate code/html. Now to read up on that smarty ;).
sako
23-Apr-2008 12:36
I don't think there is a bad time to start separating logic from presentation. It is actually a good idea to start doing this from the get-go so it becomes a habit. This is a great habit.. Everything now a days is going away from mixing logic and presentation for simplicity sake among other things (look at html and css). I think a good thing to read up on is smarty. It can do wonders separating your logic from presentation.
ducky
03-Apr-2008 01:54
To the above poster:

That's probably too much to think about when you're starting out... You should probably at first just concentrate on getting the stuff running and learning syntax before you consider best practices and the like.
rfantin at coralwood dot com
20-Dec-2006 08:00
While it's easy to get carried away mixing your logic and presentation together since it's so easy to do, your better off using PHP within HTML only to fill in values, or include other source files.

Keep your actual processing in separate libraries that are called before you send any headers to the page. Try to avoid calling a script that retrieves or sets information, or manipulates it in the middle of your HTML. You'll find it's much easier to maintain.

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