How to call PHP file in HTML
To call a PHP file in an HTML document, you typically embed the PHP code within the HTML file using the .php
extension. Here's a simple example:
Create a PHP File: Create a new PHP file, for example, example.php
, and add some PHP code:
<?php
// PHP code here
$message = "Hello from PHP!";
?>
Create an HTML File: Create a new HTML file, for example, index.html
, and include the PHP file using the include
or require
statement. Here's an example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML with PHP</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<?php
// Include the PHP file
include('example.php');
?>
<p><?php echo $message; ?></p>
<p>This is a static HTML content.</p>
</body>
</html>
Run the HTML File: Save both files in the same directory and run the HTML file. If you're using a local development environment like XAMPP or MAMP, place the files in the appropriate directory (e.g., htdocs
for XAMPP).
Open your web browser and navigate to the HTML file, for example, http://localhost/index.html
.
View Output: You should see the output of both static HTML content and the dynamic content from the PHP file.
In this example, the include('example.php');
statement is used to include the PHP file within the HTML file, and the PHP variable $message
is echoed within the HTML content. This allows you to mix PHP and HTML seamlessly.
Thank you.