ホームページ  >  記事  >  バックエンド開発  >  Hint=0 はすでに定義されていますが、なぜ if(hint=0) が必要なのでしょうか?

Hint=0 はすでに定義されていますが、なぜ if(hint=0) が必要なのでしょうか?

WBOY
WBOYオリジナル
2016-06-23 14:01:131001ブラウズ

コードは次のとおりです:

<?php$xmlDoc = new DOMDocument();$xmlDoc->load("links.xml");$x=$xmlDoc->getElementsByTagName('link');//get the q parameter from URL$q=$_GET["q"];//lookup all links from the xml file if length of q>0if (strlen($q) > 0){$hint="";for($i=0; $i<($x->length); $i++) { $y=$x->item($i)->getElementsByTagName('title'); $z=$x->item($i)->getElementsByTagName('url'); if ($y->item(0)->nodeType==1)  {  //find a link matching the search text  if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))   {   if ($hint=="")    {    $hint="<a href='" .     $z->item(0)->childNodes->item(0)->nodeValue .     "' target='_blank'>" .     $y->item(0)->childNodes->item(0)->nodeValue . "</a>";    }   else    {    $hint=$hint . "<br /><a href='" .     $z->item(0)->childNodes->item(0)->nodeValue .     "' target='_blank'>" .     $y->item(0)->childNodes->item(0)->nodeValue . "</a>";    }   }  } }}// Set output to "no suggestion" if no hint were found// or to the correct valuesif ($hint == "") { $response="no suggestion"; }else { $response=$hint; } //output the responseecho $response;?>


このコード内:
if ($hint=="")    {    $hint="<a href='" .     $z->item(0)->childNodes->item(0)->nodeValue .     "' target='_blank'>" .     $y->item(0)->childNodes->item(0)->nodeValue . "</a>";    }   else    {    $hint=$hint . "<br /><a href='" .     $z->item(0)->childNodes->item(0)->nodeValue .     "' target='_blank'>" .     $y->item(0)->childNodes->item(0)->nodeValue . "</a>";    }


質問 1: $hint=0 が以前に定義されていない場合、なぜ if ($hint=="") が必要なのでしょうか?
質問 2: $hint が =0 でない場合、次のコードは何を実行しますか?


ディスカッションへの返信 (解決策)

where $hint=0 ?

$hini="" ステートメントは、最初のループでは値を持ちませんが、2 回目のループでは値が発生します。ループ

where $hint=0 ?

$hint=""
はい、0 ではありませんが、便宜上 0 としました

$hint=""; $i の場合、これは for ループ内にあります。 = 0の場合、$hintは空文字列となりif($hint=="")判定に入りますが、ループで1を加算した場合、つまり$i = 1の場合、このときの$hintはありません。長い場合は空の文字列です。この判断は最初のループを区別するためです

$hint=""; $i = 0 の場合、$hint は空の文字列になり、if ($hint=) に入ります。 ="") の判定ですが、ループが 1 を加えたとき、つまり $i = 1 のとき、このときの $hint は空文字列ではなくなります。この判定は最初のループを区別するためのものです

実際には、判断する必要はありません
1 行目かどうかを判断するために使用します

1 行目でない場合は、その前に改行を追加します

実際には CSS に任せた方が良いです。 0c6dc11e160d3b678d68754cc175188a を追加せずにリストを改行すると、そのような判断が不要になります。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。