PHP while循环,回声问题

时间:2012-03-12 17:35:29

标签: php while-loop

必须有一种更简单的方法...... 我一直在第二行得到这个。

解析错误:语法错误,意外';'

while($row = mysql_fetch_array($result)){
    echo ("'$MAP_OBJECT->addMarkerByCoords";
          "(\"";
          $row['longitude'];
          ",";
          $row['latitude'];
          ",\"";
          $row['routername'];
          "-";
          $row['desc'];
          "\", \"";
          $row['routername'];
          "-";
          $row['desc'];
          "<br><a href=\"./div/";
          $row['routername'];
          "\">Site Info</a>'");
    echo "<br />";
}

3 个答案:

答案 0 :(得分:4)

您必须与.合并,而不是;

echo ("'$MAP_OBJECT->addMarkerByCoords" . 
      "(\"" .
      $row['longitude'] .
      ....

查看手册:http://php.net/manual/en/language.operators.string.php

答案 1 :(得分:2)

如果您尝试连接这些字符串,则大多数;应为.

while($row = mysql_fetch_array($result)){
    echo ("'$MAP_OBJECT->addMarkerByCoords" .
          "(\"" .
          $row['longitude'] .
          "," .
          $row['latitude'] .
          ",\"" .
          $row['routername'] .
          "-" .
          $row['desc'] .
          "\", \"" .
          $row['routername'] .
          "-" .
          $row['desc'] .
          "<br><a href=\"./Ldiv/" .
          $row['routername'].
          "\">Site Info</a>'"); // Here's the actual end of the statement
    echo "<br />";
}

这对于HEREDOC来说会更加整洁:

echo <<<ROW
$MAP_OBJECT->addMarkerByCoords(
   {$row['longitude']},
   {$row['latitude']},
   "{$row['routername']}-{$row['desc']}",
   "$row['routername']}-{$row['desc']}"<br>
    <a href="./Ldiv/{$row['routername']}">Site Info</a>
)

ROW;

虽然在<br>之前看起来有些东西丢失了,因为之前的报价没有关闭。

答案 2 :(得分:0)

您尝试执行的内容的语法错误。

PHP中的;本质上是一个结束语句。你告诉PHP停止在你想要的第一行echo ("'$MAP_OBJECT->addMarkerByCoords" ;上执行回声。

相反,将所有;替换为.,除了最后一个echo语句

while($row = mysql_fetch_array($result)){
echo "'$MAP_OBJECT->addMarkerByCoords" .
      "(\"" .
      $row['longitude'] .
      "," .
      $row['latitude'] .
      ",\"" .
      $row['routername'] .
      "-" .
      $row['desc'] .
      "\", \"" .
      $row['routername'] .
      "-" .
      $row['desc'] .
      "<br><a href=\"./Ldiv/" .
      $row['routername']; .
      "\">Site Info</a>'";
echo "<br />";

}