How To Use PHP In HTML

Integrating HTML with PHP is an essentail step to create dynamic websites. In this guide we will learn how to use PHP in HTML . PHP can be embeed inside HTML making possible to create a lot of possibilites . To use PHP in HML we can add HTML code in the file with PHP code . But the file should be saved with .php extension .It can be then processed properly. In this guide we will learn step by step how to use PP code within HTML data.

Prerequisites

1.Basic knowledge of HTML and PHP

2.Basic Installation on xampp in your PC

Introduction To PHP In HTML

PHP is a sever side scripting language which is used to create dynamic pages in website. It is an opensource language . It can be easily downloaded and can be used. The basic thing in php is the syntax we use to write php code. PHP code is always written within opening and closing tags like this as below:

 <?php //code here ?> 

The second important thing is the echo statement which is used to output the content in browser.

Suppose we need to display content Hello World content on browser.So we need to write below content

<?php echo "Hello World"; ?>

HTML is hypertext markup language which generates static content.But using PHP we can add dynamic elements to that HTML.

Embedding PHP code in HTML

PHP code can be embedded in HTML in many ways .The two basic ways are:

1.Adding inline php code

2.Adding conditional logic to display content

ADDING INLINE PHP CODE

PHP code can be added inline using php tags inside html content. Create a page index.php inside folder phpcodetohtml . Paste the example below:

<!DOCTYPE html>
<html lang="en">
<head>
 <title>Adding INLINE PHP Code</title>
</head>
<body>

<h1>Hello Everyone!/h1>

<?php
$name = "Dipti";
echo "<p>Hello, $name! Welcome to PHPGURUACADEMY.</p>";
?>

</body>
</html>

Here in above code we have added html structure. Inside the body tag a php variable $name is defined. This varable holds a name of person. Now we generated p tags with the variable value as dynamic content using PHP.

ADING CONDITIONAL LOGIC TO DISPLAY CONTENT

PHP can be condtionally used to display content. This is important when form submits or any other event takes place . The condition which is returned with this event is used to display html data dynamically. Like if we have a login form and we need to show the message on basis of login of user.

Check the below codefor this:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Display content using PHP conditional logic</title>
</head>
<body>

<h1>Content Below</h1>

<?php
$loggedIn = true;

if ($loggedIn) {
    echo "<p>Welcome back user!</p>";
} else {
    echo "<p>Please login to use application.</p>";
}
?>

</body>
</html>

In the above code we have variable $loggedIn = true . This checks if value is true it will show the first condition in if block . If the condition is false the second message in else condition will be shown.

PHP can be this used in many ways inside HTML code. It can also handle database related functionalities and HTML form handlings

I have created another tutorial where you can see how contact form can be created using both html and php

Test The Code

The index.php page you created inside folder phpcodetohtml . Now open url http://localhost/phpcodetohtml . You can see the generated output on screen like below:

In this guide we learned how PHP can be used in HTML code. PHP can be used in HTML to create dynamic, interactive web applications. We can embed PHP in html directly . We can use simple conditional logic to display content.PHP provides full ability to integrate dynamic functionality in HTML Pages.

Scroll to Top