You can't run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. To do this you need to create a .htaccess file in your root web directory and add this line to it:

AddType application/x-httpd-php .htm .html

This will tell Apache to process files with a .htm or .html file extension as PHP files.

Answer from John Conde on Stack Overflow
🌐
W3Schools
w3schools.com › php › php_syntax.asp
PHP Syntax
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo "My car is $color<br>"; echo "My house is $COLOR"; ?> </body> </html> Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
Discussions

Html in php files?
The webserver has an extension that manage php content. Whenever a file ending in .php is requested by a visitor, the webservers takes the PHP code between and passes it to the PHP interpreter. Then, if the code executed returned some text (maybe with aditional HTML tags) it will replace the position of the PHP tags. Once there's no more code between php tags, the resulting text file is sent to the browser as a response. The browser then interprets the structure given by HTML tags and the given content and shows it to the visitor. More on reddit.com
🌐 r/PHPhelp
7
1
February 2, 2022
How to write html code inside <?php ?> block? [closed]
I want to create a table with script. How can I insert html code into to achieve my goal? More on stackoverflow.com
🌐 stackoverflow.com
programming practices - Should I Include PHP code in HTML or HTML in PHP? - Software Engineering Stack Exchange
What is the best practice, to write my PHP code inside my HTML code or HTML inside PHP? PHP inside HMTL: Welcome to the website. HTML i... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
August 4, 2015
A Noob Question About Using HTML file vs a PHP file?
With the default setup of most webservers, php-code will only be executed in php-files, not in html files. You can change that in the webservers config or use a htaccess file to rewrite inputs before they are being executed. All of my projects use a htaccess file that treats html and php the same way. More on reddit.com
🌐 r/PHPhelp
10
2
March 24, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-use-php-in-html
How to use PHP in HTML ? - GeeksforGeeks
You simply need to enclose the PHP code with the PHP starts tag <?php and the PHP end tag ?>. The code wrapped between these two tags is considered to be PHP code, and it will be executed on the server side before the requested file is sent ...
Published   July 23, 2025
🌐
Envato Tuts+
code.tutsplus.com › home › php › php scripts
How to Use PHP in HTML | Envato Tuts+ - Code
March 26, 2022 - In this article, I'll show you how to use PHP code in your HTML pages. It’s aimed at PHP beginners who are trying to strengthen their grip on the world's most popular server-side scripting...
🌐
PHP
php.net › manual › en › faq.html.php
PHP: PHP and HTML - Manual
Use it's numerical form element ... as the index to the elements array, for example: ... Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables. It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, ...
🌐
DreamHost
help.dreamhost.com › hc › en-us › articles › 360037073252-Running-PHP-code-in-a-file-with-an-html-extension
Running PHP code in a file with an .html extension – DreamHost Knowledge Base
<html <body> <?php echo "hello"; ?> </body> </html> However, if you need to place PHP code in a file with an .html extension, you must first add a handler to your .htaccess file so the PHP code is able to function.
🌐
NTC Hosting
ntchosting.com › encyclopedia › scripting and programming › php › php in
How to use PHP in HTML pages
This will save you from typing <?php at the beginning of the code, shortening it to just <?. In order to enable this, you should update the php.ini file and turn the "short_tags" setting from "Off" to "On". While on most servers this setting is already turned on, it's always best to check beforehand. A problem that can occur if using short tags is a conflict with the XML usage. For XML, the <? syntax will start a processing function. To avoid this problem, the alternative <?= tag can be used. <html> <head></head> <body class="page_bg"> Hello, today is <?=date('l, F jS, Y'); ?>. </body> </html>
Find elsewhere
🌐
Code Institute
codeinstitute.net › blog › coding › how to use php in html
How to Use PHP in HTML | PHP vs HTML - Code Institute Global
October 18, 2022 - However, there is a chance that ... use PHP in HTML. If you want to connect PHP and HTML code, the best way is to construct a link between them....
🌐
ThoughtCo
thoughtco.com › php-with-html-2693952
Do You Know the Trick for Using HTML and PHP on the Same Page?
May 7, 2025 - With this method, you can include the HTML inside of the PHP tags. This is a good method to use for adding HTML to PHP if you only have a line or so to do. ... "; Echo "HTML With PHP"; Echo "My Example"; //your php code here Print "Print works too!"; ?> Much like the first example, PHP still works here regardless of using PRINT or ECHO to write HTML because the PHP code is still contained inside the proper PHP tags.
🌐
Reddit
reddit.com › r/phphelp › html in php files?
r/PHPhelp on Reddit: Html in php files?
February 2, 2022 -

I'm a total beginner at php development, but sometimes I see that the file is named action.php yet contains just a full html syntax document code in it....what is that all about? I know that one can run php code in an html file by using appropriate php opener-closer tags but how is it that its a php extension file but runs html?

🌐
InMotion Hosting
inmotionhosting.com › inmotion hosting home › support › website › how to enable html files to display php code
How to enable html files to display php code | InMotion Hosting
October 10, 2024 - You will need to check your php version so you know which line to comment in the previous code. If you’re running 8.1+, then comment out: FollowSymLinks Includes # Uncomment the version of PHP you have on your server · Click the Save Changes button in the upper right corner of the screen. Your html file should now display the php code within it.
Top answer
1 of 4
5

What about:

Neither

Mixing languages is not a good idea. You don't put JavaScript in HTML, or HTML in JavaScript, or JavaScript in PHP, or HTML in Python or Ruby in SQL.

Why don't we do that?

Because different languages usually correspond to different layers.

PHP deals with business logic. HTML deals with presentation of the business objects. Thus, you have two layers: the one generates the objects; the other one builds a pseudo-XML representation of those objects.

In the same way, JavaScript deals with interaction, and CSS with the presentation of the content. When you want to change interaction, you don't open an .html file, and you surely don't open a .php file: you open a .js or .coffee file.

By mixing logic within the same file, you'll quickly notice that maintaining the code becomes more and more difficult. You want to change the method which applies rebates to products, and you spend fifteen minutes browsing through a mix of HTML with PHP statements here and there, and PHP code with HTML all over it.

The fact that technically, PHP makes it possible to mix HTML and PHP is irrelevant. The feature is available for historical reasons, but shouldn't be used any longer.

So what should you do?

What are you probably looking for is called templates. Depending on the framework you use, it may already be available, usually under a form of MVC, where the template is in the view, or you may have to use a third-party template engine, such as Smarty.

In both cases, the idea remains the same. You have PHP code strictly separated from the template which contains the HTML and a bit of very simplistic logic: simple loops over entities, conditions for conditional displaying of information, etc. When the PHP code is ready, it calls the template engine, passing to it some information. The engine uses a specific template to build the final output (often HTML, but other formats are possible as well) which is then sent to the user.

By using templates, you make sure that you have a strict separation between the business logic and the form you give to this logic through the templates. One day, you can throw all those templates and write other ones, without modifying a single line of PHP code. Or you may create a bunch of templates for a mobile version of the website.

An additional benefit is that you are not even stuck with HTML. If you need to make an API which is based practically 1:1 on the actual web pages of the website, instead of applying the template, you serialize to JSON the object which was about to be sent to the template engine. With practically zero efforts, you add API capability to an app, thing which couldn't be possible without severe rewriting if you were mixing PHP and HTML.

2 of 4
1

You would have a better chance of resolving Sunni/Shite differences than you will of getting a clear answer to this sort of question.

For myself, I like React's notion of components and PHP s heredoc syntax.

class CompanyComponent
{
    public function __construct($company)
    {
        $this->company = $company;
    }
    protected function escape($string)
    {
        return htmlspecialchars($string, ENT_COMPAT | ENT_HTML5, 'UTF-8');
    }
    public public function render()
    {
        $companyName = $this->escape($this->company['name']);
        return <<<EOT
<p class="lead">Welcome to the {$companyName} website.</p>
EOT;
    }
}
$companyComponent = new CompanyComponent(['name'=>'Trump']);
echo $companyComponent->render();
🌐
Educative
educative.io › answers › php-in-html
PHP in HTML
A PHP script can easily be integrated within an HTML script and treated as HTML. On any HTML page, the PHP code is enclosed within PHP tags, <?php and ?>. When the visitor opens the page in a browser, the PHP compiler and the server processes ...
🌐
Mga
itwebtutorials.mga.edu › php › chp2 › mixing-html-php.aspx
Mixing HTML & PHP : MGA - Web Development Tutorials
January 30, 2016 - PHP is an embedded language, meaning that you can jump between raw HTML code and PHP without sacrificing readability. In order to embed PHP code with HTML, the PHP must be set apart using PHP start and end tags. The PHP tags tell the web server where the PHP code starts and ends.
🌐
3D Bay
clouddevs.com › home › php guides › using php in html: a practical guide
Using PHP in HTML: A Practical Guide
December 7, 2023 - Let’s explore these techniques. Inline PHP involves embedding PHP code directly within HTML elements using the <?php … ?> tags. This method is useful for executing small snippets of PHP code within your HTML markup.
🌐
GeeksforGeeks
geeksforgeeks.org › html › how-to-embed-php-code-in-an-html-page
How to embed PHP code in an HTML page ? - GeeksforGeeks
August 5, 2025 - Example 2: In this, we can use PHP code in different lines so that we can make a webpage according to the need and put code wherever necessary as shown in the below example ... <html> <body> <h2>Welcome to GeeksforGeeks</h2> <?php echo "Good morning"; ?> <p>Good to see you</p> <?php $str = "This is GeeksforGeeks portal"; echo " $str."; ?> </body> </html>