php variable in html no other way than: <?php echo $var; ?>
Yes, that is one way to output the value of a PHP variable in HTML.
Yes, that is one way to output the value of a PHP variable in HTML. Here are a few other options:
- Using short tags: If you have short tags enabled in your PHP configuration, you can use
<?= $var ?>to output the value of a variable. - Using a HEREDOC string: You can use a HEREDOC string to output multiple lines of HTML and include the value of a PHP variable within the HTML. For example:
Example of using a HEREDOC string in PHP
php— editable, runs on the server
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
- Using a double-quoted string: You can also use a double-quoted string to output HTML and include the value of a PHP variable within the HTML. For example:
Example of using a double-quoted string to output HTML and include the value of a PHP variable within the HTML
php— editable, runs on the server
Keep in mind that you should use single quotes (') around HTML attributes to avoid having to escape double quotes. For example:
Example of using single quotes (') around HTML attributes in PHP to avoid having to escape double quotes HTML
php— editable, runs on the server