Basic Principles of php-Websites

The programming language php has been around since 1995 and is a “classical” web development tool. Here I want to go over the very basics of the process to develop a simple online app with php. I’m doing this development on a Laptop with Ubuntu 20.10; if you are using a different operating system, some steps might differ, but this is not meant as a complete walk-through anyway.

A Very Brief Introduction to php

php is run on a server and makes it possible to construct an HTML document dynamically before sending it to the client who requested it. In the most simple form, it could look something like this:

<!DOCTYPE html>
<html>
  <head>
    <?php include 'head.html'; ?>
  </head>
  <body>
    <header>
      <?php include 'header.html'; ?>
    </header>
    <main>
      <?php include 'main.html'; ?>
    </main>
    <footer>
      <?php include 'footer.html'; ?>
    </footer>
  </body>
</html>

This is basically just an HTML document, where php is used to insert some placeholders on request. For a basic website this can already be very useful, if you want to have the same header and footer on every page, but the main content changes. All the pages can then include the same html files and thus reduce repetitions (which would be annoying to change later).

This example is very basic, but shows you how easy it is to get started with php, if you already know HTML. You can just write an HTML document, change the ending to .php and it is already valid code. Then just insert your php-code between <?php and ?> and it’s output will be inserted directly in that place.

Of cause php can do much more complex things, look at the documentation for all the details details1.

Running The Example Locally

Let’s make an even simpler example for demonstration purposes:

<!DOCTYPE html>
<html>
  <body>
    <h1>
      <?php echo "Hello World!"; ?>
    </h1>
    <p>
      <?php echo "I just created my first php website!"; ?>
    </p>
  </body>
</html>

Now, if you take above code, save it in a file called index.php and load it onto a server you rented, then you can direct your browser to the page and it probably works as expected. However, if you store the same file locally and try to open it with your browser, it will not execute the include commands. Instead, it will probably download the file for you. Why is this?

Your browser does not know how to interpret a .php file. It might seem like it does exactly that when you open a website like https://my-website.com/index.php, but what actually happens here, is that the server is running a special software, that interprets the .php file and sends HTML to your browser.

Let’s look at this locally, by installing and starting a software just like that:

$ sudo apt install php-cli
...
$ php -S localhost:8000
[Thu Mar 11 20:15:57 2021] PHP 7.4.9 Development Server (http://localhost:8000) started

Now you have started a local php server, which you can reach with your browser under the address http://localhost:8000/index.php (make sure to run php in the directory, where index.php is). You should see something like this:

The minimal php website

Pretty basic, but you see that echo does exactly what you would think it does.

What Does The Server Do?

The more interesting part is looking at the terminal again. Here three new lines appeared:

$ php -S localhost:8000
[Thu Mar 11 20:15:57 2021] PHP 7.4.9 Development Server (http://localhost:8000) started
[Thu Mar 11 20:16:10 2021] 127.0.0.1:40876 Accepted
[Thu Mar 11 20:16:10 2021] 127.0.0.1:40876 [200]: GET /index.php
[Thu Mar 11 20:16:10 2021] 127.0.0.1:40876 Closing

The server accepted a new incoming connection from port 40876, received one HTTP request (GET /index.php), then the connection was closed again. But this shows something crucial: The browser is not actually accessing the file index.php, instead it is just sending everything that follows the address after the / as a GET-request and the server decides what it does with this request. In this case, the server knows php, so it executes the php code and returns the result as HTML to the client, which is shown to you as a website.

By the way, this is also why some websites, for example amazon, have URLs like this:

https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/

You can remove the description and still access the same website using this shorter address:

https://www.amazon.com/dp/0132350882/

I highly doubt that amazon has an actual file system on its servers that reflects this structure. Instead there is probably some kind of server software that extracts the number after the /dp keyword and looks up the article number in some database. You can even add some nonsense in between and the site will still load correctly. Try this:

https://www.amazon.com/this-is-a-test-if-this-still-loads/dp/0132350882/

In Conclusion

This should allow you to develop some basic php-based websites. You should look at the php documentation1 for a proper introduction to programming php, but the basic cycle should be clear now:

  1. Write a .php script that dynamically creates a complete HTML file
  2. Run a local server with php and test your website there
  3. If you’re happy, upload it to a server that has php enabled to publish it

References


  1. https://www.php.net/docs.php ↩︎

coding_structure

A blog by Timo Dreyer


2021-03-11