I know this is a basic php question, but I'm having trouble showing variables inside an echo of HTML. Below is what I'm trying to achieve.
<?php
$variable = 'testing';
echo <span class="label-[variable]">[variable]</span>
?>
should output:
<span class="label-testing">testing</span>
<?php
$variable = 'testing';
echo <span class="label-[variable]">[variable]</span>
?>
Should be
<?php
$variable = 'testing';
echo '<span class="label-' . $variable .'">' . $variable . '</span>';
?>
If your HTML contains double quotes, then you can wrap it in single quotes:
echo '<span class="label-' . $variable . '">' . $variable . '</span>';
Additionally, you can do it inline by escaping the double quotes:
echo "<span class=\"label-$variable\">$variable</span>";
Using double quotes also allows for special characters such as \n
and \t
.
The documentation is your friend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With