PHP in HTML

PHP is a server-side scripting language. That means a PHP script is executed on the server, the output is built on the server, and the result is sent as HTML to the client browser for rendering. It is quite common to code PHP within HTML in a script.

PHP in HTML

Many complex scripts may require you to combine the features of HTML and PHP to achieve the required results. Even though they are different languages, with their own respective functions, it doesn’t change the fact that PHP is designed to interact with HTML scripts.

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 the PHP code enclosed within PHP tags. Everything within the PHP tags is processed by the PHP compiler. The server then sends the output from the PHP code to the user’s browser, where it is displayed.

php in html
php in html

Steps

  1. Open a notepad file and save it with the extension .html to create an HTML file.
  2. Edit this file to include some HTML and PHP code inside the file. For example:
<h1> This is html script </h1>
<?php

echo "hello world in php";

?>
  1. Open the .html file so that it runs on the browser.
svg viewer
  1. When we run the file, the output does not print the code in the PHP column because the .html file does not recognize the PHP notation. This is where we use an htaccessa distributed configuration file that Apache uses to handle configuration changes on a per-directory basis file.

  2. In the same folder where you have saved the .html file, open a new notepad text document and save it with the extension of htaccess.

  3. Edit the .htaccess file and add the following command. This makes sure the browser recognizes the PHP script within the HMTL script file.

AddType application/x-httpd-php .htm .html
  1. Now, reload the .html file in the browser to view the expected output.
svg viewer

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved