W3docs

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:

  1. Using short tags: If you have short tags enabled in your PHP configuration, you can use <?= $var ?> to output the value of a variable.
  2. 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

$var = "Hello World!";
$html = <<<EOT
<div>
   The value of the variable is: $var
</div>
EOT;

echo $html;

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

  1. 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

$var = "Hello World!";
echo "<div>The value of the variable is: $var</div>";

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

$var = "Hello World!";
echo "<div class='container'>The value of the variable is: $var</div>";