Chapter 1: Introduction to PHP
Version: PHP 8.3
What PHP Is
PHP is a programming language that lives on web servers. When you open a normal HTML page, the server just hands the file to your browser, exactly as it was saved. A PHP page is different. Before anything is sent back, the server runs the PHP code inside the page, and only the result of that run travels to the browser.
That one idea explains most of what PHP can do. Because the code runs fresh on every visit, a PHP page can greet you by name, show today's date, check a password, fetch products from a database, or charge a shopping cart. The page is built at the moment you ask for it, using information that is true at that moment.
PHP was created in 1994 by Rasmus Lerdorf, who wanted simple tools to manage his personal home page. The name originally stood for "Personal Home Page". It grew far beyond that. The current name is a recursive joke: "PHP: Hypertext Preprocessor". You do not need to remember any of this for an exam. What matters is the job PHP was born to do: build web pages on a server, on demand.
You have almost certainly used PHP today without knowing it:
- WordPress is written in PHP, and WordPress powers a huge share of the world's websites, from small blogs to large news sites.
- Facebook started as a PHP site. It grew so large that Facebook built its own PHP runtime, but PHP carried it through its early, fastest years.
- Wikipedia runs on MediaWiki, which is PHP.
- Laravel, one of the most loved web frameworks of the last decade, is PHP. So is Symfony, which many large companies use for their internal systems. NOTE PHP is a server-side language. JavaScript in a web page runs inside the visitor's browser. PHP runs on the server, before the page is sent. The visitor never sees your PHP code, only the HTML it produced. Chapter 2 shows exactly where the line between the two sits.
A Short History That Explains the Design
PHP's history explains why it feels the way it does. Most languages are designed first and released later. PHP grew in public, one practical problem at a time.
In 1994, Rasmus Lerdorf wrote a small set of tools in the C language to track visits to his online résumé. He called the package Personal Home Page Tools. Other people found the tools useful, asked for more features, and the package kept growing: a form interpreter was added so pages could react to submitted forms, then database support so pages could read and store real data. In 1997, two Israeli developers, Andi Gutmans and Zeev Suraski, rewrote the core parser for version 3, and the project outgrew its personal origins. The name was changed to the recursive "PHP: Hypertext Preprocessor", a small joke programmers enjoy: the first letter stands for the whole name.
Version 4 arrived in 2000, version 5 in 2004 brought a proper object model (you will meet objects in Chapter 18), and PHP 7 in 2015 made everything dramatically faster, roughly doubling the speed of typical sites overnight. PHP 8, released in 2020, added a just-in-time compiler and a batch of modern language features; PHP 8.3, the version this book teaches, is a polished release in that line.
Two lessons hide in that timeline. First, PHP was shaped by people building real websites under real deadlines, so it favours getting a page working over following a theory. You will feel that friendliness in every chapter. Second, PHP has been declared dead by commentators roughly every year since 2005, while quietly continuing to run most of the web's content sites. Judge a tool by the work it does, not by the noise around it.
Built Fresh, Every Time
A plain HTML file is finished the moment its author saves it. Every visitor gets identical bytes, whether they arrive today or next year. A PHP file holds instructions instead of a finished page, and the finished page is created at the moment of the visit. This program makes the difference visible even in a terminal:
<?php
echo "This page was generated at " . date("H:i:s") . ".\n";
echo "Run the same file again and the time changes, because the output is rebuilt on every
run.\n"; Save it as generated.php and run it twice, a couple of seconds apart:
php generated.php Output of the first run:
This page was generated at 11:22:16.
Run the same file again and the time changes, because the output is rebuilt on every run. Output of the second run, two seconds later:
This page was generated at 11:22:18.
Run the same file again and the time changes, because the output is rebuilt on every run. Same file, different output, because the file was never the page. The file is a recipe, and the page is cooked when someone orders it. The date() function in the code asks the machine for the current time in hours, minutes, and seconds; Chapter 14 teaches it properly. Almost everything PHP is used for (showing the newest products, the latest comments, the current stock level) is this one trick applied with real data instead of the clock.
Why Learn PHP?
There are four honest, practical reasons.
Jobs. Companies have been building with PHP for over 25 years, and those systems still need developers. Job boards in every country list PHP roles, especially Laravel roles and WordPress roles. A language with millions of live websites does not stop needing programmers.
WordPress. If you ever want freelance work, small business sites, or a shop website, WordPress is the fastest route from skill to income, and WordPress themes and plugins are PHP. Knowing PHP turns you from someone who clicks settings into someone who can change anything.
Cheap hosting. PHP hosting is some of the cheapest hosting on earth. Many hosts run a small PHP site for the price of a cup of tea per month, and nearly every budget host supports PHP out of the box. Learning a language you can afford to deploy is a real advantage when you are starting out.
Laravel. Once you know plain PHP, Laravel gives you a modern, elegant way to build bigger applications: logins, databases, queues, emails, all organised for you. Employers ask for it by name. This book teaches plain PHP first, on purpose, so that Laravel later feels like help rather than magic.
There is one more reason, and it is personal: PHP gives feedback fast. You write a few lines, refresh, and see a result. That quick loop makes it a friendly first server-side language.
How PHP Runs: The Request Journey
Let us follow one visit, slowly. Say a visitor opens shop.example.com/product.php in their browser.
- The browser sends a request across the internet: "please give me product.php".
- The web server (software like Apache or Nginx, or PHP's own built-in server, which we will use in this book) receives the request and finds the file on disk.
- Because the file ends in .php , the server does not send it directly. It hands the file to the PHP engine, the program that understands PHP code.
- The PHP engine runs the code from top to bottom. If the code asks for data, it talks to a database (we will use PDO with SQLite later in this book, and MySQL behaves the same way from PHP's point of view).
- The code produces HTML as its output. The server sends that HTML back to the browser, and the browser draws the page.
One visit, start to finish: the request goes in, the code runs on the server, and plain HTML comes back:
Two details in that journey are worth fixing in your mind now:
- The PHP code itself never leaves the server. If a visitor views the page source in their browser, they see only HTML. Your passwords, database queries, and logic stay private.
- Every request starts from zero. When the page finishes, PHP forgets everything and the next visitor starts clean. Remembering people between visits takes extra tools (cookies and sessions), which we build in Chapter 17. GOTCHA Beginners often try to open a .php file by double-clicking it, the way you open an HTML file. The browser then shows you the raw code, or just downloads the file. That is expected: a PHP file must go through a server (even the small one built into PHP) so the engine can run it first. You will do exactly that in Chapter 2.
Setting Up PHP
You need exactly one thing installed: PHP itself. In this book we use PHP 8.3 from the command line. There is no heavy IDE to configure and no web server package to install, because PHP ships with a small built-in server for learning and testing.
On Ubuntu or Debian Linux, install it like this:
sudo apt install php-cli On macOS, PHP can be installed with Homebrew ( brew install php ). On Windows, download the ZIP from windows.php.net , unzip it into a folder such as C:\php , and add that folder to your PATH. Whichever route you take, the test is the same. Open a terminal and run:
php -v You should see something like this (this is the real output from the machine this chapter was written and tested on):
Output:
PHP 8.3.6 (cli) (built: Oct 7 2026 18:06:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies Your build date and the last digit of the version may differ. That is fine. What you want to see is PHP 8.3 at the start. If the terminal answers "command not found", PHP is either not installed or not on your PATH yet; fix that first, because every chapter depends on it.
For writing code, any plain text editor works: VS Code is a good free choice, and PHP extensions for it add syntax colours and error hints. Avoid word processors like Microsoft Word for code; they hide formatting inside the file that PHP cannot read.
Three setup problems account for almost every stuck beginner:
- php: command not found (or "is not recognized" on Windows). PHP is not installed, or its folder is not on the PATH, the list of folders the terminal searches for programs. On Windows this is the usual issue after unzipping: add the folder that holds php.exe to the PATH in system settings, then open a new terminal.
- The version shows 7.x or older. You have an old PHP, possibly left over from a bundle installed years ago. This book's examples target 8.3, and a few of them (the match expression in Chapter 6, the string functions in Chapter 4) simply do not exist in PHP 7. Install 8.3 alongside or instead, and confirm with php -v before continuing.
- The file runs but prints the code itself. You opened the file in a browser directly, or ran it with the wrong tool. Run PHP files with php filename.php in the terminal for now; the browser route arrives in Chapter 2 with the built-in server.
Your First Script
Time to make PHP do something. Create a file named hello.php and type this into it:
<?php
echo "Hello from PHP!\n";
echo "This text was printed by a program, not typed into a page.\n"; A few things to notice before we run it:
- <?php is the opening tag. It tells the PHP engine: "code starts here". Nearly every PHP file begins with it.
- echo is PHP's way of sending text to the output. In a terminal script, the output is the terminal. In a web page, the output becomes part of the HTML.
- The text sits inside double quotes. Text like this is called a string, and Chapter 4 is devoted to strings.
- \n inside a double-quoted string means "new line". It is how we press Enter from inside code.
- Every instruction ends with a semicolon. Forget it and PHP complains loudly; Chapter 2 shows the exact complaint so you can recognise it.
Now run it from the terminal, in the folder where you saved the file:
php hello.php Output:
Hello from PHP!
This text was printed by a program, not typed into a page. That is a complete PHP program: saved in a file, run by the engine, producing output. When we run it in a browser in the next chapter, the same echo idea will build real pages.
One more tiny program. PHP can answer questions about itself with a built-in function called phpversion() . A function is a named piece of ready-made ability; we use many built-in ones in this book and write our own in Chapter 8.
<?php
echo "You are running PHP version " . phpversion() . "\n"; Output:
You are running PHP version 8.3.6 The dots join pieces of text together (Chapter 4 explains joining in detail). Note that phpversion() returned the exact version installed, not something we typed. That is the pattern of all PHP work: your code asks the engine and the wider system for true, current facts, then prints answers built from them.
Try It Yourself
- Create a file introduce.php that prints three lines: your name, your city, and one sentence saying you are
learning PHP. Use one echo per line, and remember the \n at the end of each string.
Answer
<?php
echo "My name is Abdur Rahman.\n";
echo "I live in Sylhet.\n";
echo "I am learning PHP.\n"; Output:
My name is Abdur Rahman.
I live in Sylhet.
I am learning PHP.