tomcraig:
If the search string is in the first position (i.e. position[0]), then your coditional will resolve to false.
anonymous:
"The function should return TRUE on any non-zero value." The strpos() function never returns true. It returns, as daniel says, either false or an integer. That includes 0, which evaluates to false if using "==". Also note that the manual text (as you yourself quote it) says that, in the case of an unsuccessful search, the if expression evaluates to true, not that the strpos() function returns true (which, again, it never does).
Mark:
"Try it with == TRUE. It works fine..." Sure it does, as long as the search string is not in the first position.
The manual and daniel are correct.
実用的な例
次に、より実用的なことをしてみましょう。 ページを見ているユーザが使用しているブラウザの種類を確認してみます。 これを行なうには、ブラウザが 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 文以上のことを行なうコードブロックを作成することができます。 例えば、インターネット・エクスプローラかどうかを調べたい場合は、 以下のようにします。
<?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, else と strtoupper() や 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 ブロックだけが見る側に送信されることになります。
実用的な例
16-May-2008 05:33
15-May-2008 07:11
No, "double negative" is indeed correct. Just as in C and C++, False evaluates to 0 and True is defined as "Not False".
Try it with == TRUE. It works fine, and is easier to understand.
14-May-2008 11:37
Daniel, you are incorrect:
From the text of the tutorial above:
.....If it does not return FALSE, the if expression evaluates to TRUE .....
So the poster of the double-negative comment is absolutely correct. The function should return TRUE on any non-zero value.
10-May-2008 08:52
In response to the "Double Negative" post.
I think you missed the line where it says that the function does not return 'true' it returns a position. So you cannot evaluate == true you must evaluate !== false because strpos() only returns a number or false.
09-May-2008 06:25
As an instructor of computer languages, I think your use of a double negative is a poor choice and bad example. Here is how I would code your example:
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == TRUE)
{
echo 'You are using Internet Explorer.<br />';
}
?>
Now instead of saying something "if something is not FALSE", you are saying if "something is TRUE". Having programmed for over 30 years, I have found this much easier to code, debug and pass on to future programmers.
22-Apr-2008 02: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.
03-Apr-2008 03: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.
20-Dec-2006 10: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.
