Shiphp The PHP Developer's Guide to Docker

Making Your First Website with PHP

In order to build a working website on your computer that is powered by PHP, you’ll need to first have PHP installed and know some basic HTML. In this tutorial we will make a website that displays variables written in PHP code to users in an HTML document. Let’s get started!

  1. Open a text editor and create a file called index.html
  2. Add the following code to your new file:
<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My PHP Website</title>
</head>

<body>
  <h1>My PHP Website</h1>
  <p>Here is some static content.</p>
</body>
</html>

This is HTML code, which can be seen in a web browser. You can open this file in Google Chrome or Firefox and you should see a website like this:

It’s not very pretty, but it’s a simple website!

Right now this website is static, meaning that there’s not a server or any PHP code powering it. Next, we’ll convert this simple static web page into a dynamic one with PHP:

  1. Rename your index.html file to index.php
  2. Open a command line terminal and navigate to the index.php file.
  3. PHP comes with a built-in web server. We’ll use this to try out our new webpage by running php -S localhost:8000
  4. Now open http://localhost:8000 in your web browser. You should see the same thing we saw before with the static HTML page.
  5. Open the index.php file in a text editor. Below the first <p> tags, create a new set of <p> tags and add the following PHP code:

Now when you refresh http://localhost:8000 you will see a new line. This line was generated in PHP, and now our site is truly dynamic!

References

1. index.html

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My PHP Website</title>
</head>

<body>
  <h1>My PHP Website</h1>
  <p>Here is some static content.</p>
</body>
</html>

2. index.php

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My PHP Website</title>
</head>

<body>
  <h1>My PHP Website</h1>
  <p>Here is some static content.</p>
  <p><?php echo "Here is some dynamic content"; ?></p>
</body>
</html>

Like this Post? Get My eBook for FREE!

Learn to build your first Dockerized PHP application.

In this book, PHP developers will learn everything they need to know to start building their applications on Docker, including:

  • Installing dependencies using Composer.
  • Getting data from a third-party API.
  • Saving data to a MySQL database.
  • Using a web framework (SlimPHP) for routing.
  • Storing environmental variables securely.
  • And much more!

You can buy this book on Leanpub or sign up for my mailing list to get a coupon code to download it for FREE.

Get a FREE Coupon Buy it Now