These are the setup notes from the book, kept current here. If an installer screen has changed since your copy was printed, this page is the version to follow.
Three things have to be in place before any program in this book will run: PHP itself, an editor to write code in, and a terminal to run it from. All of it is free, and the setup takes about fifteen minutes. Do it once and you will not have to think about it again. Chapter 1 begins on the assumption that it is done.
Install PHP
Windows does not come with PHP. Recent versions of macOS do not include it either, so a Mac needs an installation of its own as well. The language is the same in every case. Only the way you get it differs.
Windows
The simplest route is a bundle: Laragon, from laragon.org, or XAMPP, from apachefriends.org. Each of the two installs PHP, a web server and MySQL in one go, which saves you assembling the same set later. The first of them puts the php command where a terminal can find it. XAMPP keeps its copy in C:\xampp\php, and that folder has to go on your PATH before the command works outside XAMPP's own tools.
To install the language on its own instead, take the zip file from windows.php.net/download, any of the builds listed there runs this book, and unpack it to C:\php. Then put that folder on your PATH: press the Windows key, type environment, open Edit the system environment variables, click Environment Variables, select Path, and add C:\php. Close every terminal window afterwards, since a terminal reads the PATH only when it starts.
macOS
Install Homebrew from brew.sh if you do not have it, then type brew install php in Terminal. Homebrew puts the php command on your PATH itself, and it is also the least painful way to update PHP later.
Linux
On Ubuntu or Debian, sudo apt install php-cli is the whole job, and Fedora uses sudo dnf install php-cli. The package named php-cli is the command-line PHP that Chapters 1 to 9 run on.
Whichever route you took, check the result in a terminal. If you have never opened one, read Open a Terminal below first.
Windows PowerShell:
PS C:\Users\you> php --version
PHP 8.4.3 (cli) (built: Feb 11 2025 17:04:03) (NTS)
Copyright (c) The PHP Group
macOS Terminal:
~ % php --version
PHP 8.4.3 (cli) (built: Feb 11 2025 17:04:03) (NTS)
Your version number and build date will be different, and that is fine. Anything from 8.1 upward runs every program in this book. If the terminal says instead that it does not know the command, When Something Goes Wrong covers that.
| System | Simplest route | Check |
|---|---|---|
| Windows | Laragon or XAMPP. | php --version |
| macOS | Homebrew, then brew install php. | php --version |
| Linux | sudo apt install php-cli. | php --version |
Choose an Editor
You also need somewhere to write the code. A word processor will not do. It saves formatting along with the text, and it turns straight quotation marks into curly ones, which PHP will not accept inside a program.
Visual Studio Code
Download VS Code from code.visualstudio.com and accept the defaults. It is what I use. Two habits are worth forming on the first day. Open a folder rather than a single file, with File, then Open Folder, so that your whole project sits in the sidebar. And type the file name yourself when you save, the ending included: hello.php. One extension is worth adding on top of that, PHP Intelephense, which completes function names as you type. Nothing here depends on it.
Notepad++
Notepad++, from notepad-plus-plus.org, is a Windows-only plain-text editor that opens instantly and does very little else. Watch its save dialog. Set Save as type to All types and type the full name, hello.php, or you get hello.php.txt, and PHP will report that the file does not exist.
PhpStorm
PhpStorm, from jetbrains.com, is the full professional PHP environment, and it is paid software once the trial ends. It is worth knowing the name. It is not what I would start on.
| Editor | Best for | Where to get it |
|---|---|---|
| VS Code | Everything in this book. Free, and the one I recommend. | code.visualstudio.com |
| Notepad++ | A small, fast Windows editor with nothing to configure. | notepad-plus-plus.org |
| PhpStorm | Professional PHP work later on. Paid. | jetbrains.com |
Open a Terminal
A terminal is a plain window where you type one command, press Enter, and read what the computer prints back. It is not a programming language. It is the place where you start programs.
On Windows, press the Windows key, type terminal, and open Windows Terminal, which starts PowerShell inside it. On an older Windows, type cmd and open Command Prompt instead. Either one runs everything in this book, and Laragon's main window has a Terminal button that opens one with PHP already on the PATH. On macOS, open Terminal from the Utilities folder inside Applications, or press Command-Space, type terminal, and press Enter.
The line the terminal shows you is the prompt, and it names the folder you are standing in. Move to another folder with cd, short for change directory.
Windows PowerShell:
PS C:\Users\you> cd C:\phpbook
PS C:\phpbook>
macOS Terminal:
~ % cd ~/phpbook
phpbook %
Every command in this book is run from the folder that holds the file, so cd is the one you will type most often after php itself.
Write and Run Your First Script
- Make one folder for your work: C:\phpbook on Windows, or a folder named phpbook in your home folder on macOS. Every file from this book goes in it, or in a folder for each chapter underneath it.
- In your editor choose File, then New, and type the two lines below. Save the file into that folder as
hello.php. Type the .php ending yourself, since it is part of the name rather than a setting in the dialog.
<?php
echo "Hello from PHP";
Now open a terminal, move into that folder with cd, and run the file by name.
Windows PowerShell:
PS C:\phpbook> php hello.php
Hello from PHP
macOS Terminal:
phpbook % php hello.php
Hello from PHP
The first line of each pair is what you type. The line under it, with no prompt in front of it, is what your program printed. That is what a RUN step means everywhere in this book: save the file, run it, and compare what you see with what you predicted.
From Chapter 10 on, a file answers a request from a browser rather than printing into a terminal, and PHP carries a small web server for exactly that. Start it now, in the same folder, so that the command is familiar when the chapter needs it.
PS C:\phpbook> php -S localhost:8000
[Mon Jan 6 09:15:02 2026] PHP 8.4.3 Development Server
(http://localhost:8000) started
[Mon Jan 6 09:15:20 2026] 127.0.0.1:52114 [200]: GET /hello.php
Leave that window open, then open a browser and go to http://localhost:8000/hello.php. The page reads Hello from PHP in plain text, since the script produces no HTML. The terminal adds a line for the request, and the 200 in it is HTTP for the file was found and sent. Press Ctrl+C there to stop the server. It serves the folder it was started in, so start it where your files are.
When Something Goes Wrong
Five things go wrong on a first afternoon, and all five have short answers.
| What you see | What it means |
|---|---|
'php' is not recognized as an internal or external command |
Windows cannot find PHP. Either php.exe sits in a folder that was never added to PATH, or the terminal was open before you added it, so close it and open a new one. With XAMPP the folder is C:\xampp\php. On macOS the same trouble reads command not found: php. |
Parse error: syntax error, unexpected token "echo", expecting "," or ";" |
PHP could not read the file as a program, usually because of a missing semicolon or an unclosed quotation mark. Read the line number in the message, then look at the line above it: PHP notices the omission only when the next statement stops making sense. |
Could not open input file: hello.php |
The file is not in the folder you are standing in, so move there with cd. If it is plainly there, it was saved as hello.php.txt, which is a different name. Turn on File name extensions in the View menu of File Explorer. |
| A blank or half-finished page in the browser | PHP stopped on a fatal error and this installation has display_errors switched off, so the message never reached the page. The terminal running php -S printed it. Running the same file with php hello.php prints it too, and ini_set("display_errors", "1"); at the top of the file brings it back to the page. |
Failed to listen on localhost:8000 (reason: Address already in use) |
Another server already holds that port, usually one you started earlier and left running. Press Ctrl+C in its window, or start this one on another port, 8001 for instance, and use that number in the address as well. |
The Companion Website
The files that go with this book are free at tprmway.com/php: printable cheat sheets, extra exercises, starter files for the four projects in Chapter 14, and any corrections made after printing. Nothing in the book depends on them. Tools change faster than languages. If a program name or download address in this section has moved on by the time you read it, the website keeps a current version of these setup notes.