【问题标题】:<a href=…> syntax error unexpect something [closed]<a href=...> 语法错误意外的东西[关闭]
【发布时间】:2014-03-28 08:10:58
【问题描述】:
   <?  $i = 0;
            foreach( $model->authors as $key => $author){
            ?>
                <?
                //if (++$i < count($model->authors)) echo $author->name.';'; else echo $author->name.' ';
                if(++$i<count($model->authors)) 
                {
                    <a href="http://192.168.171.46:9090/search/index?keyword=<? echo $author->name; ?>"><? echo $author->name.';' ?></a>;
                }else echo 
                    <a href="http://192.168.171.46:9090/search/index?keyword=<? echo $author->name ?>"><? echo $author->name.' ' ?></a>; 



                 ?>
            <? }
        ?>

但它显示这一行语法错误 unexpect

【问题讨论】:

  • &lt;?php ?&gt; 永远不要使用 starters 的简写,否则你的 else 会缺少一个左括号,以及大量其他语法错误。

标签: php html


【解决方案1】:

试试这个:

<?php
$i=0;
foreach($model->authors as $key=>$author) { 
    if(++$i<count($model->authors)) {
        echo '<a href="http://192.168.171.46:9090/search/index?keyword='.$author->name.'">'.$author->name.';</a>';
    } else {
        echo '<a href="http://192.168.171.46:9090/search/index?keyword='.$author->name.'">'.$author->name.' </a>';
    }
}

或者,如果您不介意稍作改动,请使用它,因为它更简单:

<?php
$ret='';
foreach($model->authors as $key=>$author) 
   $ret.=($ret?'; ':'').'<a href="http://192.168.171.46:9090/search/index?keyword='.$author->name.'">'.$author->name.'</a>';
echo $ret;

【讨论】:

    【解决方案2】:

    假设您正在编写 PHP,请这样做:

    <?php  
    $i = 0;
    foreach( $model->authors as $key => $author){
        if(++$i<count($model->authors)) {
            echo "<a href='http://192.168.171.46:9090/search/index?keyword=" . $author->name . "'>" . $author->name . "</a>";
        } else {
            echo "<a href='http://192.168.171.46:9090/search/index?keyword=" . $author->name . "'>" . $author->name . "</a>"; 
        }
    }
    ?>
    

    【讨论】:

      【解决方案3】:
      <?php
      $i = 0;
      foreach( $model->authors as $key => $author){
          if(++$i<count($model->authors)){
              ?>
              <a href="http://192.168.171.46:9090/search/index?keyword=<?php echo $author->name; ?>"><?php echo $author->name.' '; ?></a>;
              <?php
          }
          else{
              ?>
              <a href="http://192.168.171.46:9090/search/index?keyword=<?php echo $author->name; ?>"><?php echo $author->name.' '; ?></a>
              <?php
          }
      }
      ?>
      

      我看到你对语法很困惑——也许你应该考虑至少在一开始就使用带有语法检查的编辑器?

      【讨论】:

        【解决方案4】:
        <?
            $i = 0;
            foreach ($model->authors as $key => $author):
                if (++$i < count($model->authors)): ?>
                    <a href="http://192.168.171.46:9090/search/index?keyword=<?= $author->name ?>"><?= $author->name ?>;</a>
                <? else: ?>
                    <a href="http://192.168.171.46:9090/search/index?keyword=<?= $author->name ?>"><?= $author->name ?> </a>
                <? endif;
            endforeach;
        ?>
        

        或者更短的版本。

        <?
            $i = 0;
            foreach ($model->authors as $key => $author): ?>
                    <a href="http://192.168.171.46:9090/search/index?keyword=<?= $author->name ?>"><?= $author->name,++$i < count($model->authors) ? ';' : ' ' ?></a>
                <?
            endforeach;
        ?>
        

        【讨论】:

          猜你喜欢
          • 2013-07-21
          • 2012-03-10
          • 1970-01-01
          • 2013-11-11
          • 2013-01-10
          • 2012-02-26
          • 2012-10-03
          • 2014-04-02
          • 2011-10-26
          相关资源
          最近更新 更多