Thakkennes
Thakkennes

Reputation: 215

How can I put HTML tags in a PHP variable?

I am trying to make a variable which contains some HTML tags, this isn't working like I want it to. I was hoping someone could tell what I am doing wrong here.

My Code:

$foto = "put picture here";
$naam = 'Sieraad1';
$prijs = '20,00';

$artikel = '<img src="'$foto'"><h4>'$naam'</h4><h6>€'$prijs'</h6>';

echo '<table><tr><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td></tr>';
echo '<table><tr><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td></tr>';
echo '</table>';

Upvotes: 0

Views: 17789

Answers (5)

mjsolo
mjsolo

Reputation: 315

try

$artikel = "<img src=\"'$foto'\"><h4>'$naam'</h4><h8>€'$prijs'</h8>";

or

$artikel = '<img src="' . $foto . '"><h4>' . $naam . '</h4><h8>€' . $prijs .'</h8>';

then just echo the $artikel - you don't need the htmlspecialchars

Upvotes: 0

YamaCasis
YamaCasis

Reputation: 1

easy to use

<?php
echo <<< END
<table class="head"><tr>
  <td class='head'>$name</td>
  <td>$fname</td>
 </tr></table>
END;
?>

or

<?php
echo "<table class='head'><tr>
  <td class='head2'>$name</td>
  <td class='head3'>$fname</td>
 </tr></table>";
?>

Upvotes: 0

sandip patil
sandip patil

Reputation: 658

var concatenation is missing for variable $artikel. replace user code line with below:

$artikel = '<img src="'.$foto.'"><h4>'.$naam.'</h4><h8>€'.$prijs.'</h8>';

Upvotes: 0

poke
poke

Reputation: 388463

Using htmlspecialchars on html code will convert < to &lt;, > to &gt; and " to &quot;. So it will obviously break your code. Run the htmlspecialchars on the inner contents instead:

$artikel = '<img src="' . htmlspecialchars($foto) . '"><h4>' . htmlspecialchars($naam) . '</h4><h8>€' . htmlspecialchars($prijs) . '</h8>';
echo '<table><tr><td>'.$artikel.'</td>...';

Upvotes: 0

Uku Loskit
Uku Loskit

Reputation: 42050

$artikel = '<img src="'$foto'"><h4>'$naam'</h4><h8>€'$prijs'</h8>';

You are missing . here between the variables for concatenation.

Upvotes: 2

Related Questions