Open In App

How to use PHP in HTML ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will use PHP in HTML. There are various methods to integrate PHP and HTML, some of them are discussed below.

You can add PHP tags to your HTML Page. 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 to the client browser.

Note: To use PHP in HTML, you have to use the .php extension because In PHP the code is interpreted and run on the server-side.

Syntax:

<html>
<?php echo "welcome to geeksforgeeks" ?>
</html>

Examples to use PHP in HTML

Example 1: In this example, we are rendering a simple h2 tag with the help of PHP code.

PHP
<!DOCTYPE html>
<html>
    <body>
        <center>
            <h2>
                <?php 
            echo "This is PHP code inside html"
            ?>
            </h2>
        </center>
    </body>
</html>

Output:

PhpHtml

PHP in HTML Example Output


Example 2: In this example, we are using different PHP code snippet to render on web page.

PHP
<!DOCTYPE html>
<html>

<head>
   
</head>

<body>
    <center>
      

        <p>
          <?php 
            echo "Complete <strong>Portal</strong> for Geeks."
          ?>
        <br><br>
        <?php
        echo 'Explore, learn and grow.'
        ?>
        </p>
    </center>
</body>

</html>

Output:

PhpHtml2

PHP in HTML Example Output



Last Updated : 22 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads