Rumah > pembangunan bahagian belakang > tutorial php > 已经定义hint=0为啥还要if(hint=0)

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-06-23 14:01:13
asal
1138 orang telah melayarinya

代码如下:

<?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;?>
Salin selepas log masuk


这段代码中:
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>";    }
Salin selepas log masuk


问题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来做比较好,不用加
,也就省去这样判断了。

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan