A language for building websites
PHP is a programming language designed for the web. You write PHP code in files that end in .php, put them on a web server, and the server runs them whenever someone requests the page. The code can read the address the visitor asked for, look things up in a database, check a login, do calculations, and then print the HTML that the visitor's browser will display.
That is the key difference from HTML and CSS. An HTML file is sent to the browser exactly as it is written. A PHP file is a small program that produces a page, so the page can be different for every visitor and every request: a shop shows the products that are in stock right now, a forum shows the newest posts, an account page shows your own name.
What happens when someone opens a PHP page
Suppose a visitor types the address of a page called hello.php. This is what happens, step by step:
- The browser sends an HTTP request for
/hello.phpto the web server. - The server (for example Apache or Nginx) sees the
.phpextension and hands the file to PHP. - PHP runs the code from top to bottom. Anything the code prints becomes part of the response.
- The server sends that response back, usually as HTML.
- The browser displays the HTML. It never sees the PHP code itself.
Here is a very small PHP file. The part between <?php and ?> is PHP; everything else is ordinary HTML that is passed through unchanged:
<!doctype html>
<html>
<body>
<h1>Today is <?php echo date('l'); ?></h1>
</body>
</html>If a visitor opens this on a Monday, the browser receives <h1>Today is Monday</h1>. On Tuesday it receives a different page, even though nobody edited the file. That is what "dynamic" means.
PHP outside the browser
PHP is not only for web pages. The same language runs from a terminal with the php command, which is handy for scripts, scheduled jobs and learning. Most examples in this course are run that way, because the output is plain text that is easy to read. A tiny script:
<?php
echo "PHP is running.\n";
echo "2 + 3 = " . (2 + 3) . "\n";PHP is running.
2 + 3 = 5The lessons in the PHP on the web section then run the same kind of code inside a web server and show the real responses.
What PHP is used for
PHP has been around since the mid-1990s and powers a very large part of the web. Some well-known software written in it:
- WordPress, the content management system behind a huge number of blogs and business sites.
- Laravel and Symfony, popular frameworks for building web applications and APIs.
- Drupal, Joomla, Magento and WooCommerce, for sites and online shops.
- Many hosting control panels and internal business tools.
Because almost every shared hosting plan supports it, PHP is also one of the cheapest ways to put a dynamic website online. This site's own admin panel and blog are built with Laravel, a PHP framework.
How PHP relates to HTML, CSS, JavaScript and MySQL
It helps to know which tool does which job:
| Technology | Runs where | Job |
|---|---|---|
| HTML | Browser | The structure and content of the page |
| CSS | Browser | How the page looks |
| JavaScript | Mostly the browser | Behaviour after the page has loaded |
| PHP | Server | Building the page and handling data before it is sent |
| MySQL | Server | Storing data such as users, products and orders |
A typical PHP page reads data from MySQL, mixes it into HTML, and sends the result. JavaScript can then make that page interactive. If you have done the MySQL course, the last section of this course connects the two.
Modern PHP
PHP has changed a great deal. Version 7 made it roughly twice as fast as version 5, and PHP 8 added features such as match expressions, named arguments, enums, readonly properties and a just-in-time compiler. Old tutorials often show habits that are now discouraged, such as the removed mysql_ functions or building SQL by gluing strings together. This course teaches current PHP (version 8.2 and newer) and points out the old patterns so you can recognise them in existing code.
Common mistakes
- Opening a .php file directly in the browser by double-clicking it. The browser shows the raw code or downloads it, because no server ran it. You need PHP to run the file, which the next two lessons set up.
- Thinking visitors can read your PHP code. They only see the output. That is also why secrets such as database passwords belong in PHP files or environment settings, never in HTML or JavaScript.
- Learning from very old tutorials. If a tutorial uses
mysql_connect(), it predates PHP 7 and its advice on security is out of date.
Exercise
Without running anything yet, answer these: (1) A page price.php prints the current price of a product from a database. Which part of the system stores the price, which part reads it, and which part displays it? (2) Why does a visitor never see the database password written in price.php?
Show one possible answer
(1) MySQL stores the price, PHP reads it and prints HTML that contains it, and the browser displays that HTML. (2) The password is inside the PHP code, which runs on the server. Only the output of the script is sent to the browser, and the script never prints the password.
Key points
- PHP is a programming language that runs on the server. The browser never sees PHP code, only the output it produces.
- A request for a .php page makes the server run the script; whatever the script prints becomes the response.
- PHP can also run from the command line, which is how most examples in this course are run.
- WordPress, Laravel, Drupal and many hosting control panels are written in PHP.