0

未定義の変数でエラー ログを詰まらせないように、PHP 変数を定義することになっていることはわかっていますが、私のログはまだ不要な情報でいっぱいになっています。

例えば...

[28-Jan-2013 16:45:42 UTC] PHP Notice:  Undefined index: site in C:\Sites\FLCBranson.org\freedownloads.php on line 34

私のPHPコードは$site定義されていますが、それをオーバーライドするオプションが必要です...

// it's a good idea to define the variable first and then make changes as necessary (that way you don't fill up your logs with worthless errors)
$site = "flc";
// overrides the domain (useful for IP addresses)
if ($_GET["site"]) $site = $_GET["site"];

だから、私はそのタイプの問題をたくさん持っています。次に、これらの厄介なエラーがたくさんあります...

[28-Jan-2013 16:45:42 UTC] PHP Notice:  Undefined offset: 3 in C:\Sites\FLCBranson.org\listseries.php on line 264
[28-Jan-2013 16:45:42 UTC] PHP Notice:  Undefined offset: 4 in C:\Sites\FLCBranson.org\listseries.php on line 265
[28-Jan-2013 16:45:42 UTC] PHP Notice:  Undefined offset: 5 in C:\Sites\FLCBranson.org\listseries.php on line 266

さまざまなコンテンツが取り込まれた配列があります。スロットに何かが入っていたら、それをどうにかしたい...

// explode() takes a string of text ($item->title in this case) and creates an array comprised of parts of the text separated by the separator (- in this case)
$title = explode(" - ", $sermontitle);
// sets the sermon title variable
$sermontitle = $title[0];
if ($title[1]) $sermontitle = $sermontitle . " - " . $title[1];
if ($title[2]) $sermontitle = $sermontitle . "<br>" . $title[2];
if ($title[3]) $sermontitle = $sermontitle . " - " . $title[3];
if ($title[4]) $sermontitle = $sermontitle . " - " . $title[4];
if ($title[5]) $sermontitle = $sermontitle . " - " . $title[5];

それで、私は間違って何をしていますか?変数を定義します。次に、特定の条件が満たされた場合にのみ変数を変更します。それが適切な方法だと思いました。

編集...

別の奇妙な例を見つけました...

[28-Jan-2013 20:07:05 UTC] PHP Notice:  Undefined variable: broadcast in C:\Sites\FLCBranson.org\flconlineservices.php on line 242

それだけでif (file_exists($golive) || ($broadcast == "live")は十分ではないようです。する必要がありますif (file_exists($golive) || (isset($broadcast) && $broadcast == "live"))か? これは、単純な比較を実行するための大量のコードのように思えます。

編集 2...

だから、なぜisset()必要なのか理解し始めていますが、ここに私が得られないことがあります。データベースから情報を取得するコードがいくつかありますif ($row["Sarasota"])が、エラーログにはそのための単一のものが表示されません。に必要な場合、なぜisset()そこに必要ないのでしょうif ($title[5])か? 私が見ることができる唯一の違いは、引用されていない数字の 5 とは対照的に、引用された単語 "Sarasota" です。

4

4 に答える 4

3

これらの通知を回避するには isset を使用します。

if (isset($title[1]) && $title[1]) $sermontitle = $sermontitle . " - " . $title[1];
if (isset($title[2]) && $title[2]) $sermontitle = $sermontitle . "<br>" . $title[2];
if (isset($title[3]) && $title[3]) $sermontitle = $sermontitle . " - " . $title[3];
if (isset($title[4]) && $title[4]) $sermontitle = $sermontitle . " - " . $title[4];
if (isset($title[5]) && $title[5]) $sermontitle = $sermontitle . " - " . $title[5];
于 2013-01-28T17:30:13.967 に答える
3

デフォルト値を書き込む一般的な方法は、次の? :ように三項演算子を使用することです。

$site = isset($_GET["site"]) ? $_GET["site"] : null; // null is the default value

配列の場合は、countではなく 関数を使用することをお勧めしissetます。これにより、配列を扱っているという読者の理解が深まります。

issetは、連想配列 ( など) で使用されるより多くのコメントで$_GETあるため、必ずしも数値ではない他のキーが予想される場合があります。

于 2013-01-28T17:31:16.083 に答える
2
if ($_GET["site"])

でなければなりません

if (isset($_GET["site"]))

また、$title については isset を使用する必要があります。

if (isset($title[1])) [...]
于 2013-01-28T17:30:45.007 に答える
1

!empty を使用して、isset のエラー処理を含むソリューションを使用することもできます。

if(!empty($_GET['site']))
于 2013-01-28T17:31:57.223 に答える