Many beginners often ask if you can just convert your HTML to PHP, failing to understand that PHP is a scripting language. You can change the file extension from .html to .php – but this does not necessarily mean that you will be able to use PHP code in your files.
Your server, or the host where your website is uploaded, will also need to have PHP installed and enabled for you to be able to use PHP code in your files. How to setup your server to use PHP will not be covered in this Article – you should contact your host to find out whether they support PHP.
Using HTML Inside PHP
You can use HTML inside of PHP, this is done using PHP echo to output the source HTML. This method may however not be the most efficient way to use PHP.
<?php echo '<!DOCTYPE html> <html lang="en-US"> <head> <title>My first Website</title> </head> <body> <p>My first Website.</p> </body> </html>'; ?>
You do normally not have to output any special mime-type for your files to become HTML, the files will automatically have the content type header set to text/html, but in case the output shows as plain text, try adding the below before sending output to the browser.
<?php header('Content-type: text/html'); ?>
See also: List of Content-types
Using PHP Inside of HTML
Another common way to use PHP, is to escape in and out of HTML using the special PHP opening and closing tags.
<!DOCTYPE html> <html lang="en-US"> <head> <title>My first Website</title> </head> <body> <?php echo ' <p>This paragraph is outputted using PHP echo</p>'; ?> </body> </html>