PHP

Share

What is PHP?

PHP [php.net] is a programming language used mainly to create websites.

Advantages of PHP

The webpages that your web browser displays for you are made out of HTML code. Originally, web servers simply stored a static HTML file that the web browser downloaded to display. This means the webpage could never change or look different for two people. With PHP, it's possible to write a program that generates different HTML code on a per-request basis. This means the same webpage can look different if you are logged in or logged out, for example.

This is all of course possible without PHP. You could do it with any other programming language, like Python. But for that, you need to use a web framework like Django or Flask, and that means you'll have to learn a templating language like Jinja. The beauty of PHP is that it requires no framework or setup. Each request starts a program to generate the HTML, the program terminates after the request is completed, and there is templating supporting built into the language.

Developers that come from other web frameworks may have trouble using PHP at first because it's a very low-level way of generating HTML.

In other frameworks, when a view function is invoked, the whole HTML of the response is generated and THEN it's sent. In PHP, the output is sent to the client the INSTANT it's outputted by default. This has a few surprising consequences.

First, if a website has a static header with a dynamic body (e.g. list of posts) that requires a database connection, and that connection is extremely slow, you can have a scenario where the client can instantly download the start of the webpage, and the web browser WILL display it, while still waiting for the PHP to fetch the data from the database in the server! If you ever see an old website that seems "stuck" loading in the middle for some reason, that's not AJAX, that's PHP.

Second, HTTP headers are, by definition, headers, which means they come BEFORE the body of the request. This means you can't output HTML and change headers later, because the body will be literally sent already. In particular, you can't start rendering a page then notice there was an error and output a 5XX status code, nor can you realize the post doesn't exist and output a 404.

Third, you similarly can't access cookies after you have already outputted HTML.

As you can imagine, these limitations are very annoying. You get a faster time to first byte, but that is it. There are built-in functions in PHP to save the output to a variable instead of outputting it, but that kind of removes one of the few good things about PHP. Large PHP websites don't use raw PHP, they use a framework like Laravel or WordPress, and that framework may also come with its own templating language. It's a bit ironic, isn't it?

Examples of PHP Code

Basic Web 2.0

The following snippet generates HTML to tell a user their IP address:

<?php
  // in PHP, all variables start with $.
  $ip = $_SERVER['REMOTE_ADDR'];
?><!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>IP finder</title>
<p>Your IP address is <code><?= $ip ?></code>.

Note: this doesn't work if the user is behind a proxy or the web server is behind a reverse proxy. You'll need a different header for that.

HTTP Headers

PHP has built-in functions for setting HTTP headers and status codes.

<?php
   status_header(418);
   header("Content-Type: application/json");
?>{
    "message": "I'm a teapot."
}

Comments

Leave a Reply

Leave your thoughts! Required fields are marked *