PHP:在IF和ELSE语句中传递URL参数
我试图在IF和ELSE语句中传递URL参数。这就是初始代码在仅使用链接而不使用参数时的样子:PHP:在IF和ELSE语句中传递URL参数,php,if-statement,parameters,url-parameters,Php,If Statement,Parameters,Url Parameters,我试图在IF和ELSE语句中传递URL参数。这就是初始代码在仅使用链接而不使用参数时的样子: <?php if ($row_checklist_finding_final['item_class'] == "Major") { echo '<a href="UserNCR.php">Go to NCR</a>'; } else { echo '<a href="UserOFI
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
echo '<a href="UserNCR.php">Go to NCR</a>';
}
else
{
echo '<a href="UserOFI.php">Go to OFI</a>';
}
?>
我还尝试删除冗余的
,如下所示:
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
<a href="UserNCR.php?item_id= echo $row_checklist_finding_final['item_id'];">Go to NCR</a>
}
else
{
'<a href="UserOFI.php">Go to OFI</a>';
}
?>
但仍然存在错误。
我用错语法了吗?
我应该如何在IF和ELSE语句中传递URL参数?如下更改代码
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
echo ' <a href="UserNCR.php?item_id='.$row_checklist_finding_final["item_id"].'">Go to NCR</a>';
}
else
{
echo '<a href="UserOFI.php">Go to OFI</a>';
}
?>
您可以使用。连接运算符将变量连接在一起,如中建议的 另一种方法是对字符串使用双引号,因为双引号允许包含变量。执行此操作时,阵列需要用花括号包围:
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
echo "<a href=\"UserNCR.php?item_id={$row_checklist_finding_final['item_id']\">Go to NCR</a>';
}
else
{
echo '<a href="UserOFI.php">Go to OFI</a>';
}
?>
该示例可能易受HTML注入/攻击,并且在某些情况下可能生成无效的HTML。见“
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
echo ' <a href="UserNCR.php?item_id='.$row_checklist_finding_final["item_id"].'">Go to NCR</a>';
}
else
{
echo '<a href="UserOFI.php">Go to OFI</a>';
}
?>
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
echo "<a href=\"UserNCR.php?item_id={$row_checklist_finding_final['item_id']\">Go to NCR</a>';
}
else
{
echo '<a href="UserOFI.php">Go to OFI</a>';
}
?>