Introduction to PHP

PHP is a programming language designed for building websites. It runs on the web server, where it reads requests, talks to databases and generates the HTML that browsers display. It powers WordPress, Wikipedia, Laravel and Symfony applications and a large share of the web. This course teaches modern PHP 8.5: typed, object-oriented, and a pleasure to write.

What PHP looks like

Here is a complete PHP script. It stores some prices in an array, adds them up and prints a summary:

PHP
<?php

$prices = [4.50, 12.99, 3.25];
$total = array_sum($prices);

echo count($prices) . " items, total " . number_format($total, 2) . "\n";
Output
3 items, total 20.74

Variables start with a dollar sign, statements end with a semicolon, and PHP code lives between <?php and the end of the file. You'll run scripts like this yourself in lesson 3.

How PHP works on the web

When a browser requests a page such as /products.php, the web server hands the file to PHP. PHP runs the script from top to bottom, and whatever the script outputs — usually HTML — is sent back as the response. Then the script ends, and the next request starts fresh.

PHP products.php
<?php
$products = ['Notebook', 'Pen', 'Backpack'];
?>
<h1>Our products</h1>
<ul>
<?php foreach ($products as $p): ?>
  <li><?= $p ?></li>
<?php endforeach; ?>
</ul>
HTTP
GET /products.php
Responses
> GET /products.php
<h1>Our products</h1>
<ul>
  <li>Notebook</li>
  <li>Pen</li>
  <li>Backpack</li>
</ul>

The browser never sees the PHP code — only the HTML it produced. In this course, web examples show each request (> GET /products.php) followed by the response the browser receives.

Why learn PHP?

  • Made for the web. Reading form data, handling cookies and sessions, sending headers and talking to databases are built in.
  • Runs everywhere. Practically every web host supports PHP, and deploying can be as simple as uploading files.
  • Huge ecosystem. WordPress, Drupal, Laravel, Symfony and hundreds of thousands of Composer packages.
  • Modern and fast. PHP 8 brought a JIT compiler, strict types, enums, readonly properties, match expressions and much more. Today's PHP is a very different language from the PHP of 2005.
  • Gentle learning curve. You can make a working web page in minutes, then grow into professional, well-structured code.

A short history

YearMilestone
1994–1995Rasmus Lerdorf writes "Personal Home Page Tools" to track visits to his online CV.
1998PHP 3 — rewritten by Andi Gutmans and Zeev Suraski; the name becomes the recursive "PHP: Hypertext Preprocessor".
2004PHP 5 adds a real object model.
2015PHP 7 roughly doubles performance and adds scalar type declarations.
2020–2023PHP 8.0–8.3: named arguments, match, attributes, enums, readonly properties, fibers and a JIT compiler.
2024PHP 8.4: property hooks, asymmetric visibility and new array functions.
2025PHP 8.5: the pipe operator |>, clone with new property values, array_first()/array_last() and a standards-compliant URI parser.

A new version comes out every November. Each version gets two years of active support plus two of security fixes, so keep your PHP reasonably up to date.

How this course works

Each lesson explains one topic with short, complete scripts and their exact output. Every example was run with PHP 8.5 — command-line scripts with the php command, web examples through PHP's built-in web server — and the output you see is what it produced. (File paths in error messages are shortened to just the file name.) Most lessons end with a Try it yourself editor: change the code, press Copy, and run it on your own computer.

Next: install PHP so you can run every example yourself.