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 OverflowYou 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.
I think writing PHP into an .html file is confusing and anti-natural. Why would you do that??
Anyway, if what you want is to execute PHP files and show them as .html in the address bar, an easiest solution would be using .php as normal, and write a rule in your .htaccess like this:
RewriteRule ^([^.]+)\.html
1.php [L]
Html in php files?
How to write html code inside <?php ?> block? [closed]
programming practices - Should I Include PHP code in HTML or HTML in PHP? - Software Engineering Stack Exchange
A Noob Question About Using HTML file vs a PHP file?
Videos
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?
You can do like
HTML in PHP :
<?php
echo "<table>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>".$name."</td>";
echo "</tr>";
echo "</table>";
?>
Or You can write like.
PHP in HTML :
<?php /*Do some PHP calculation or something*/ ?>
<table>
<tr>
<td>Name</td>
<td><?php echo $name;?></td>
</tr>
</table>
<?php /*Do some PHP calculation or something*/ ?>
Means:
You can open a PHP tag with <?php, now add your PHP code, then close the tag with ?> and then write your html code. When needed to add more PHP, just open another PHP tag with <?php.
You can drop in and out of the PHP context using the <?php and ?> tags. For example...
<?php
$array = array(1, 2, 3, 4);
?>
<table>
<thead><tr><th>Number</th></tr></thead>
<tbody>
<?php foreach ($array as $num) : ?>
<tr><td><?= htmlspecialchars($num) ?></td></tr>
<?php endforeach ?>
</tbody>
</table>
Also see Alternative syntax for control structures
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.
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();
You did not clarify, so my best guess would be that your file has a .html extension.
You may combine PHP and HTML in a single document, but you typically need to give it a .php extension in order for the code to be parsed. There is also the .phtml extension used by some, but I typically try to avoid using it for anything but Views (Google MVC if you really want to know).
Rename your index.html to index.php and that should take care of it. Unless this is done already, then please update us.
Just Create a .htaccess file in your root directory and then copy following line and then save.
AddHandler application/x-httpd-php5 .html .php .htm
Now code within any .html or .htm files will work as php
so you dont need to change extension from .html to php