这下面是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 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>
例 $a=array("1");
$a[]="2";
此时$a的值就为array("1","2");
明白了吗?