Sadegh
Sadegh

Reputation: 325

How to use echo inside echo

I Have this code:

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

I have to use echo inside echo and again I have to never use php in this line. haw can I edit this line and make it like:

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

EDIT: thanks every one for very useful help. now I have another problem.what can i do with this line:

echo'<input type="hidden" class="pname" value="<?php echo $product->product_name ?>">';

Upvotes: 0

Views: 1834

Answers (6)

you don't have to use an echo inside another, you can do one of the following:

<?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 
}
?>

or you can use concatenation:

$varsecondsentence = "second sentence";

echo "this is the fisrt sentence, " . $varsecondsentence

this will echo : this is the fisrt sentence, second sentence

Upvotes: 2

Amal Murali
Amal Murali

Reputation: 76646

Variables in PHP are expanded (interpolated) inside double quotes. So you don't need to use echo inside your echo statement. It's incorrect -- both syntactically and logically.

The following should work:

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

Or just use string concatenation, like so:

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

All these do the same thing, but I recommend using the first approach since it's cleaner.

Upvotes: 1

user2533777
user2533777

Reputation:

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

Upvotes: 6

jonas vermeulen
jonas vermeulen

Reputation: 1245

just concat var and string should work, then you only need one echo

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

take a look in these pages for more documentation http://php.net/manual/en/language.operators.string.php

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

Upvotes: -2

AgeDeO
AgeDeO

Reputation: 3157

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

Upvotes: 1

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

You do not need to echo inside echo all you need is proper concatination.

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

OR

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

if php tag is opened before then close it and then try above solution.

Such as

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

Upvotes: 4

Related Questions