在 php 中回显链接,并在 <img> 的 alt 中插入 php


Echo link in php, and insert php in alt of <img>

我有一个针对特定产品的HTML标签<img>,但是有些产品没有图像,因为它们的类似产品已经具有相同的图像。我希望访问这些无图像产品的查看者能够单击链接并访问类似的产品。现在<img>是这样的:

<img src="../imgs/product/<?php echo $productName;?>.png" alt="No image for this item. Please refer to its similar item: <?php echo $alt; ?> "/>

然后我希望$alt部分是类似产品的链接,所以我写了:

<?php 
    $alt = '<a href='"singleproduct.php?singleItem=' 
         . $similarProduct .''">'. $similarProduct. '</a>'; 
?>

无论我如何修改引号的样式,都无法打印出链接。只是想知道是否根本不可能在<img>里面插入<a>......?如果没有,如何做到这一点?

在 html 属性中不能有 html 标记。如果您想要完整的 html 回退,请查看输入默认图像以防 html 的 src 属性无效?