Reputation: 36
I wants to know if someone have an idea of how can we call a variable inside html code like if we are using the smarty template.
<?php
$var = "sometext";
?>
<html>
<body>
<?php echo $var ?>
</body>
</html>
If we are using smarty template we can do like this:
<?php
$var = "sometext";
?>
<html>
<body>
{$var}
</body>
</html>
Is that possible without using smarty templates? Thank you for help!
Upvotes: 1
Views: 607
Reputation: 912
You can't use Smarty syntax in 'naked' PHP... that's why it's Smarty syntax.
You have three options:
<?php echo $var; ?>
or <?= $var; ?>
(there are good reasons not to use the second, mostly that it's difficult to comment out in PHP. You have to use HTML comments which leave remnants of PHP in your HTML source.)preg_match
to search for a pattern like /\{(.*?)\}/
and replace them with the variables, but beware that code replacement causes complexities. If you want to go this route, look into output buffering and regex.Note: in PHP 5.4 or newer, even if short tags are disabled, the short echo tag will work. Therefore, as long as you're not running unsupported old versions of PHP, you don't have to worry about access to it.
Upvotes: 2