How can PHP code be properly displayed and executed within a template?

To properly display and execute PHP code within a template, you can use the `<?php ?>` tags to encapsulate your PHP code. Make sure that your template file has a `.php` extension so that the PHP interpreter can process the PHP code within it. Additionally, ensure that your web server is configured to interpret PHP code.

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;PHP Template Example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;PHP Code Execution&lt;/h1&gt;
    &lt;?php
        $message = &quot;Hello, World!&quot;;
        echo &quot;&lt;p&gt;$message&lt;/p&gt;&quot;;
    ?&gt;
&lt;/body&gt;
&lt;/html&gt;