Dan Q's PHP Cookery Class

An intro to PHP for Web Revivalists

Lesson 1: Introduction

Objectives

  • Work out whether you're ready to start writing PHP
  • Understand the difference between static HTML and PHP-enhanced HTML
  • Know that when you see a <?php block in a php file, you're looking at PHP code that will be run on the server, not be sent to the visitor's browser

Prerequisites

You will need the following, which it's assumed you've already got:

PHP's place on the Web

Fundamentals of static HTML

Suppose you put a static HTML file on your server called mypage.html. When somebody goes to https://yoursite.example.com/mypage.html, the Web server sends a copy of the file to your visitor's browser. The browser renders the HTML (turns it from code into something visual), runs any JavaScript, applies any CSS, and displays the result.

<p>
  This is my static web page!
</p>

<p>
  I hope you enjoy my site!
</p>

Everything happens in the visitor's browser. They can "view source" to see how everything works. They can save a copy and edit it for themselves. And nothing they do will affect anybody else who visits your site: everybody will always get a copy of the same page with the same content, every single time.

How PHP is different

Now suppose you rename that file to mypage.php. When somebody goes to https://yoursite.example.com/mypage.html, your Web server does an extra step before sending anything to your visitor. It looks through your file to see if it contains any PHP code. If it does, it runs that code, replacing it with anything that code outputs. Then it sends the result to the visitor's browser.

Your visitor still gets HTML, JavaScript, and CSS as usual. But before they do, you get the chance for your code to run. And because it runs on your server, it can do things that your visitor's browser can't (or that you don't want to trust them to do!). You can use PHP to:

How PHP is written

So long as your server is configured to run PHP, you can get started by creating writing a file with a .php extension, and writing PHP code inside it.

PHP code has a special syntax to set it apart from regular HTML. A block of PHP code begins with <?php and ends with ?>. For example:

<p>
  This is my web page with PHP in it!
</p>

<?php
// 👆 This is the start of a PHP block.

// This is a comment!
// It's inside a PHP block so it won't be sent to the visitor,
// unlike a HTML comment which can be seen with "View Source".

/*
 * This is also a comment.
 * The 'echo' line below is a PHP command that outputs the text
 * "Hello, World!" to the visitor's browser.
 */
echo "Hello, World!";

// 👇 This is the end of the PHP block.
?>

<p>
  I hope you enjoy my site!
</p>

Developing locally

PHP requires a compatible web server to run it. Unlike HTML, you can't just drag a PHP file to your browser and see the result.

If you want to develop and test PHP code locally, you'll need to install a local web server on your own computer. There are lots of ways to do this, but for most people the easiest is to install XAMPP. XAMPP is a free and open-source tool that includes a web server (Apache) preconfigured with PHP capabilities. It also includes lots of other features that are beyond the scope of this class!

The next lesson includes code that you can use to test that PHP is working correctly, whether on your own computer or on your personal website's server.