如何在php脚本

时间:2017-12-29 02:45:42

标签: php html

我试图在这个显示html链接的php脚本中包含一个php变量。我需要的是在href html链接之后包含我的php $ row ['vin']变量?将值传递给我链接到的页面。顶级代码块工作,但我仍然需要PHP变量,底部是我尝试过的一个不起作用的例子。

工作但是错过了我的php变量:

 <?php if($row['instock'] == "Yes")
    {
         echo '<a href="orderForm.php?">
         <span class="glyphicon glyphicon-plus" aria-hidden="true">
         </span>
         </a>';
    }
    ?>

不起作用:

<td>
<?php if($row['instock'] == "Yes")
{
      echo '<a href="orderForm.php?'. $row['vin']">
      <span class="glyphicon glyphicon-plus" aria-hidden="true">
      </span>
      </a>';
}
?>
</td>

2 个答案:

答案 0 :(得分:3)

您已经忘记了其余的连接逻辑。它只是一个语法错误:

<td>
<?php if($row['instock'] == "Yes")
{
      echo '<a href="orderForm.php?'. $row['vin'] . '">
      <span class="glyphicon glyphicon-plus" aria-hidden="true">
      </span>
      </a>';
}
?>
</td>

答案 1 :(得分:1)

使用PHP来echo大部分静态内容的替代方法的替代方法是在必要时放入和退出PHP上下文(即<?php ... ?>)并将其用作模板语言

例如

<td>
  <?php if ($row['instock'] == "Yes"): ?>
    <a href="orderForm.php?<?= htmlspecialchars($row['vin']) ?>">
      <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
    </a>
  <?php endif ?>
</td>

请参阅http://php.net/manual/control-structures.alternative-syntax.php