1. PHP Introduction
What you will learn
- What PHP is and what it is used for
- How PHP differs from HTML, CSS, and JavaScript
- How a PHP page reaches the visitor
What is PHP?
PHP is a programming language that runs on a web server. It builds web pages on the fly: it can read from a database, check a login, process a form, and then send plain HTML to the visitor’s browser.
The name began as “Personal Home Page” and is now a recursive joke: PHP: Hypertext Preprocessor.
Server side versus browser side
| Language | Runs where? | Job |
|---|---|---|
| HTML | Browser | Structure and content |
| CSS | Browser | Appearance |
| JavaScript | Browser (usually) | Interaction in the page |
| PHP | Server | Builds the page, handles data and forms |
A visitor never sees your PHP code. They only receive the HTML it produces.
What happens when a page is requested
- The visitor’s browser asks the server for
hello.php. - The server runs the PHP code in that file.
- The PHP output, normally HTML, is sent back.
- The browser shows it, just like any other page.
A first look
PHP code: hello.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My first PHP page</title>
</head>
<body>
<h1><?php echo "Hello, world!"; ?></h1>
</body>
</html>
What the browser receives (the PHP has been replaced by its output)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My first PHP page</title> </head> <body> <h1>Hello, world!</h1> </body> </html>
The PHP section sits between <?php and ?>. Everything outside it is ordinary HTML, sent as it is.
You cannot run this file by double-clicking it. The server must process it. Lesson 2 shows how to do that on your own computer in a few minutes.
What is PHP used for?
- Websites with changing content, such as blogs and shops.
- Processing and validating form data.
- Logins, sessions, and user accounts.
- Talking to databases such as MySQL.
- Building APIs that return JSON.
Many well-known systems are written in PHP, including WordPress, the platform running this very website.
Why learn PHP?
- It is widely supported. Almost every web host runs PHP.
- It is beginner friendly, with a gentle start.
- It is the language behind WordPress themes and plugins.
- It is free and open source.
Before you start
It helps to know HTML first. Knowing a little JavaScript is useful too, because ideas such as variables, conditions, loops, and functions carry across, though the syntax differs.
Common mistakes
- Opening a
.phpfile straight from a folder. Without a server you will see the raw code, not the result. - Saving a file as
.htmland expecting PHP to run in it. Use.php. - Thinking visitors can read your PHP source. They cannot, but they can see the HTML it outputs.
- Confusing PHP with JavaScript. PHP finishes running before the page reaches the browser.
Exercise
In your own words, explain why a visitor to a PHP page cannot see the PHP code. Then name two jobs PHP can do that plain HTML cannot.
Show solution
The server runs the PHP and sends only the result (normally HTML) to the browser, so the PHP source never leaves the server.
Two jobs: processing a submitted form, and reading data from a database to build the page. Logins and sessions are other correct answers.
Knowledge check
1. Where does PHP run?
On the web server.
2. What file extension do PHP pages normally use?
.php
3. Which well-known system is written in PHP?
WordPress.
Summary
- PHP is a server-side language that builds pages and handles data.
- The visitor receives only the output, never the PHP code.
- PHP code sits between
<?phpand?>in a.phpfile.