如何在回声中使用回声


How to use echo inside echo

我有这个代码:

echo '<form method="post" class="product" action="index.php" id="addtocartproduct<?php echo $products->virtuemart_product_id ?>">';

我必须在 echo 中使用 echo,同样我必须永远不要在这一行中使用 php。我可以编辑这一行并使其如下所示:

echo '<form method="post" class="product" action="index.php" id="addtocartproduct echo $products->virtuemart_product_id ">';

编辑:感谢每个人非常有用的帮助。现在我有另一个问题。我可以用这行做什么:

echo'<input type="hidden" class="pname" value="<?php echo $product->product_name ?>">';
echo '<form method="post" class="product" action="index.php" id="addtocartproduct '. $products->virtuemart_product_id.' ">';
你不需要

在回显内部回显,你所需要的只是正确的连接。

echo '<form method="post" class="product" action="index.php" id="addtocartproduct
          '.$products->virtuemart_product_id.' ">';

<form method="post" class="product" action="index.php" 
    id="addtocartproduct<?php echo $products->virtuemart_product_id; ?>">

如果之前打开了PHP标签,请将其关闭,然后尝试上述解决方案。

?>
<form method="post" class="product" action="index.php" 
    id="addtocartproduct<?php echo $products->virtuemart_product_id; ?>">

不必在另一个回声中使用回声,则可以执行以下操作之一:

<?php 
if (somethng) {
?>
<some tags you need in plain html> <?php echo "something you need from php"; ?> </some tags you need in plain html>

<?php
}else{
?>
<some more tags you need in plain html> <?php echo "something else you need from php"; ?> </some more tags you need in plain html>
<?php 
}
?>

或者您可以使用串联:

$varsecondsentence = "second sentence";
echo "this is the fisrt sentence, " . $varsecondsentence

这将呼应:this is the fisrt sentence, second sentence

echo '<form method="post" class="product" action="index.php" id="addtocartproduct' . $products->virtuemart_product_id . '">';?>

PHP 中的变量在双引号内扩展(插值)。因此,您无需在echo语句中使用echo。这是不正确的 - 无论是语法上还是逻辑上。

以下方法应该有效:

echo "<form method='post' class='product' action='index.php' 
id='addtocartproduct.{$products->virtuemart_product_id}'";

或者只使用字符串连接,如下所示:

echo '<form method="post" class="product" action="index.php" 
id="addtocartproduct' . $products->virtuemart_product_id . '">';

所有这些都做同样的事情,但我建议使用第一种方法,因为它更干净。

只需连接var和字符串就可以了,那么你只需要一个回显

 echo '<form method="post" class="product" action="index.php" id="addtocartproduct' + $products->virtuemart_product_id+' ">';

查看这些页面以获取更多文档http://php.net/manual/en/language.operators.string.php

http://be1.php.net/manual/en/function.echo.php