已經定義hint=0為啥還要if(hint=0)

來源:互聯網
上載者:User
代碼如下:
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="item(0)->childNodes->item(0)->nodeValue .     "' target='_blank'>" .     $y->item(0)->childNodes->item(0)->nodeValue . "";    }   else    {    $hint=$hint . "
item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . ""; } } } }}// 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="item(0)->childNodes->item(0)->nodeValue .     "' target='_blank'>" .     $y->item(0)->childNodes->item(0)->nodeValue . "";    }   else    {    $hint=$hint . "
item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . ""; }


問題1:之前不是已經定義$hint=0了,為啥還要if ($hint=="")?
問題2:如題$hint不=0,後面的代碼在執行什嗎?


回複討論(解決方案)

where $hint=0 ?

聲明$hini=“” 是為了儲存用的,第一次迴圈的時候$hini是沒有值的,但是第二次迴圈的時候就有了啊

where $hint=0 ?

$hint=""
是 ,不是0,但是為了方便我說成是0了

$hint="";是在for迴圈之外定義的;在$i = 0的時候$hint是空的字串會進入到if ($hint=="")判斷中 但迴圈加1時也就是$i = 1時此時的$hint已經不再是空的字串;這個判斷就是為了區別第一次迴圈

$hint="";是在for迴圈之外定義的;在$i = 0的時候$hint是空的字串會進入到if ($hint=="")判斷中 但迴圈加1時也就是$i = 1時此時的$hint已經不再是空的字串;這個判斷就是為了區別第一次迴圈
其實可以不用判斷的

這是用來判斷是不是第一行,不是第一行的話前面加
換行
其實列表換行這種事,交給CSS來做比較好,不用加
,也就省去這樣判斷了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.