PHP Introduction
PHP is one of the most widely used server-side scripting languages in the world. It powers millions of websites — from small personal blogs to massive platforms like WordPress, Facebook (in its early days), and Wikipedia. If you want to build dynamic, data-driven websites, PHP is one of the best places to start.
What is PHP?
PHP stands for Hypertext Preprocessor (it was originally called "Personal Home Page"). It is an open-source, server-side scripting language designed specifically for web development.
- PHP code runs on the web server, not in the user's browser.
- The server processes the PHP code and sends only plain HTML to the browser.
- It can connect to databases (like MySQL) to store and retrieve data.
- PHP files use the .php extension.
- It is free to use and runs on almost every web server and operating system.
Note: Because PHP runs on the server, website visitors cannot see your PHP source code — they only see the final HTML output that the server sends.
What Does PHP Actually Do?
PHP handles the "behind the scenes" work that makes websites feel alive and personalised. Here are some things PHP does every day on the web:
Server-Side Logic
Processes requests & builds pages
Database Access
Reads & writes data (e.g. MySQL)
Dynamic Content
Personalised pages per user
- User login & registration — check if a username and password match what is stored in the database.
- Dynamic page content — show a different home page depending on who is logged in.
- Form handling — collect data from a contact form and store it or email it.
- File uploads — accept images or documents from users and save them to the server.
- Session & cookie management — keep users "remembered" across pages.
- Generating files — create PDFs, CSV exports, or images on the fly.
How PHP Works — Step by Step
Understanding the flow of a PHP request helps everything else make sense. Here is what happens when someone visits a PHP-powered page:
- The visitor types a URL into their browser and presses Enter.
- The request travels over the internet to the web server.
- The server detects a
.php file and passes it to the PHP engine.
- PHP runs the code — it may read from a database, perform calculations, or apply logic.
- PHP produces an HTML output and sends it back to the browser.
- The browser displays the HTML — it has no idea PHP was involved at all.
Tip: This is the key difference between PHP and JavaScript. JavaScript runs in the browser (client-side), while PHP runs on the server (server-side) before the page ever reaches the browser.
Your First PHP Script
Every PHP script begins with <?php and ends with ?>. Let us write the classic first program — displaying a simple message on screen:
<?php
echo "Hello, World!";
?>
When the server runs this file, it produces the following output in the browser:
Hello, World!
Note: The echo statement is how PHP sends text (or HTML) to the browser. You will use it constantly throughout your PHP journey.
PHP Inside HTML
One of PHP's most powerful features is that you can mix PHP code directly inside an HTML file. The PHP parts are processed by the server, and the final result is a clean HTML page:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Welcome to My Website</h1>
<?php
$name = "Alice";
echo "<p>Hello, " . $name . "! Welcome back.</p>";
?>
</body>
</html>
The browser receives this as pure HTML:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Welcome to My Website</h1>
<p>Hello, Alice! Welcome back.</p>
</body>
</html>
Notice: The $name variable and echo statement are completely invisible to the visitor. All they see is the resulting HTML paragraph.
Why Learn PHP?
- Beginner-friendly — PHP has a simple, readable syntax that is easy to pick up, especially if you already know some HTML.
- Massive community — millions of developers use PHP, so finding help, tutorials, and libraries is effortless.
- Powers WordPress — over 40% of all websites on the internet run on WordPress, which is built entirely in PHP.
- Works everywhere — PHP runs on Linux, Windows, and macOS and is supported by virtually every shared hosting provider.
- Great job market — PHP developers are consistently in demand for back-end and full-stack web development roles.
- Rich built-in library — PHP comes with thousands of built-in functions for strings, dates, math, file handling, databases, and more.
Key Points to Remember
- PHP is a server-side language — it runs on the server, not the browser.
- All PHP variables start with a dollar sign
$, for example: $username.
- PHP statements end with a semicolon
; — forgetting it is one of the most common beginner mistakes.
- PHP is case-sensitive for variable names but not for function names or keywords.
- Comments in PHP use
// for single-line or /* ... */ for multi-line.
Good to know: You do not need to close the PHP tag ?> at the end of a file that contains only PHP code. In fact, leaving it out is considered best practice because it prevents accidental whitespace from being sent before headers.