Learn
PHP and HTML
PHP in HTML

We can embed PHP scripts within HTML documents with the opening tag <?php and the closing tag ?>. The PHP processor will read the entire file, evaluate any PHP, translate it into HTML, and pass it off to the web server so it can be sent to the client.

Consider the following code:

<html> <head> <title>My First PHP Site</title> </head> <body> <?php echo "<h1>Oh hi!</h1>"; ?> </body> </html>

In the code above, the line echo "<h1>Oh hi!</h1>" will generate an HTML header of Oh hi!.

When we use echo within HTML we’re no longer printing to the terminal, rather we’re outputting to the HTML document.

Wouldn’t it have been simpler to just add <h1>Oh hi!</h1> directly? Yep. This example certainly doesn’t show us why we’d want to use PHP within our HTML. As we learn to develop more robust PHP scripts and harness some of the language’s more complex features, we’ll grow to understand how powerful it can be.

For now, let’s get a better understanding of how we can use PHP within HTML.

Instructions

1.

You’ll notice that the workspace now has a file titled index.php instead of index.html. We’re using the php extension because we want to add some PHP code to it.

Currently, this page is the same as a static HTML document. Add a new paragraph using PHP and echo.

The paragraph should say This is PHP.

Folder Icon

Take this course for free

Already have an account?