有人能告诉我这个简单的PHP有什么问题

时间:2012-08-15 04:15:46

标签: php

    <?php
  header("Content-type: text/xml");
  $names = array (
   "John Smith", "John Jones", "Jane Smith", "Jane Tillman",
   "Abraham Lincoln", "Sally Johnson", "Kilgore Trout",
   "Bob Atkinson","Joe Cool", "Dorothy Barnes",
   "Elizabeth Carlson", "Frank Dixon", "Gertrude East",
   "Harvey Frank", "Inigo Montoya", "Jeff Austin",
   "Lynn Arlington", "Michael Washington", "Nancy West" );
if (!$query) {
   $query=$_GET['query'];
}
echo "<?xml version=\"1.0\" ?>\n";
echo "<names>\n";
while (list($k,$v)=each($names)) {
   if (stristr($v,$query)) {
      echo "<name>$v</name>\n";
   }
}
echo "</names>\n";
?>

正如您所看到的,PHP只是在数组中查找名称 我把这个php文件放在我的xampp root中,然后在我的浏览器中输入search.php?query = John,然后它告诉我
此页面包含以下错误: 第1行第2行的错误:文档末尾的额外内容 下面是第一个错误之前的页面呈现。
怎么了?

4 个答案:

答案 0 :(得分:1)

请改为尝试:

 <?php
  header("Content-type: text/xml");
  $names = array (
   "John Smith", "John Jones", "Jane Smith", "Jane Tillman",
   "Abraham Lincoln", "Sally Johnson", "Kilgore Trout",
   "Bob Atkinson","Joe Cool", "Dorothy Barnes",
   "Elizabeth Carlson", "Frank Dixon", "Gertrude East",
   "Harvey Frank", "Inigo Montoya", "Jeff Austin",
   "Lynn Arlington", "Michael Washington", "Nancy West" );
var $query = NULL;

if (isset($_GET['query'])) {
   $query=$_GET['query'];
}
echo "<?xml version=\"1.0\" ?>\n";
echo "<names>\n";
while (list($k,$v)=each($names)) {
   if (stristr($v,$query)) {
      echo "<name>$v</name>\n";
   }
}
echo "</names>\n";
?>

答案 1 :(得分:1)

我认为问题在于 -

if (!$query) {
   $query=$_GET['query'];
}

当我运行你的代码时,上面的代码出错了。代码块不会执行任何操作,因为$ query似乎没有设置。

尝试此操作并确保设置$ _GET [“query”]变量:

  $query=NULL;
    if (isset($_GET["query"])) {
       $query=$_GET['query'];
    }

答案 2 :(得分:0)

在使用之前,您必须初始化变量$query = null;

答案 3 :(得分:0)

更改

if (!$query) {
   $query= $_GET['query'];
}

if (!isset($query)) {
   $query= $_GET['query'];
}