Home  >  Article  >  Backend Development  >  已经定义hint=0为啥还要if(hint=0)

已经定义hint=0为啥还要if(hint=0)

WBOY
WBOYOriginal
2016-06-23 14:01:131001browse

代码如下:

<?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=“” 是为了存储用的,第一次循环的时候$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来做比较好,不用加
,也就省去这样判断了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn