这下面是PHP教程上的例程代码,虽则有很详细的解释但是还是有许多地方不明白,可以看这个网址:http://www.w3school.com.cn/php/php_ajax_suggest.asp
1、$a[]即是数组为何要定义这么多不直接定义$a array["Anna","Diana",....," "]; 不就是了,如果不为数组,所以的变量都一个样(这时应叫常量吧)都是$a这怎么区别啊,此一问
2、$q=$_GET["q"];这个["q"]应当就是那个url=url+"?q="+str中的str吧,
3、这里怎么就莫名的出了$a[]值了for ($i=0;$i<count($a);$i++) {
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))同时这句的作用是什么
   这个$a[]值是上面那么多的那个值啊,都是名字相同的变量<html>
<body>
<?php
//Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";//get the q parameter from URL:
$q=$_GET["q"];//lookup all hints from array if length of q>0如果存在从JavaScript送来的文本(strlen($q)>0),则
if (strlen($q)>0)
{
$hint="";
for ($i=0;$i<count($a);$i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint."  ,  ".$a[$i];
}
}
}
}//Set output to "no suggestion" if no hint were found 
//or to the correct values
if ($hint=="")
{
$response="no suggestion";
}
else
{
$response=$hint;
}//output the respone
echo $response;
?>
</body>
</html>

解决方案 »

  1.   

    $a[]是申明数组的一种方法,$a[]实在数组$a[]中添加元素
    例 $a=array("1");
    $a[]="2";
    此时$a的值就为array("1","2");
    明白了吗?
      

  2.   

    谢谢roro这下我懂了,上面这一串数据其实就等同于$a=array("Anna","Brittany","Cinderella"..."Vicky");那第一个问题解决了
      

  3.   

    3、这里怎么就莫名的出了$a[]值了for ($i=0;$i <count($a);$i++) { 没明白你的意思,这句意思是遍历数组,count为数组长度。
      

  4.   

    4 if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) strtolower 字符串转为小写;substr截取字符串,从开头到$q的长度为止。即 "Abcd"=="aBcdefg" 成立。
      

  5.   

    现在基本弄懂了,就是html页面输入如vicky那么通过Javascript传送获了到php服务器中如果有vicky的数据值,那么就向html返回这个数组并显示在suggestion后面 ,不过我还是没有出现这样的效果,在输入域里输入时