Chapter 01 ยท Getting Started
Setting Up Your Workshop
Before a carpenter builds a chair, they set up a workshop: a table, a saw, a hammer. Before we write PHP, we set up ours: a server to run PHP, and an editor to write it in. This chapter gets your computer 100% ready.
Think of a website like a restaurant. The browser (Chrome, Firefox) is the dining table where food is served. But food is not cooked at the table โ it's cooked in the kitchen. PHP is a kitchen language: it runs on a server, prepares the "food" (HTML), and sends the finished plate to the browser. So to run PHP on your own computer, we must install a small kitchen: that's what XAMPP is.
What we are installing
| Tool | What it is | Why we need it |
|---|---|---|
| XAMPP | A free bundle containing Apache (a web server), PHP (our language), and MySQL/MariaDB (our database) | It turns your computer into a mini web server โ your kitchen |
| VS Code | A free code editor from Microsoft | Where we will actually type our PHP code |
| A browser | Chrome, Firefox or Edge | To view the pages our PHP produces |
Step 1 โ Install XAMPP
- Go to apachefriends.org and download XAMPP for your operating system (Windows, Linux or macOS).
- Run the installer. You can accept all the default options โ just keep clicking Next.
- On Windows, XAMPP installs itself into
C:\xampp. Remember that folder โ we will live there.
Step 2 โ Start Apache and MySQL
- Open the XAMPP Control Panel (search for "XAMPP" in your Start menu).
- Click Start next to Apache. Its name should turn green.
- Click Start next to MySQL. It should also turn green.
- Open your browser and visit
http://localhost. If you see the XAMPP welcome page โ congratulations, your kitchen is open for business! ๐
The most common cause is another program (often Skype or IIS) already using "port 80" โ think
of a port as a door number, and two programs cannot use the same door. Easiest fix: close the
other program, or in XAMPP click Config โ Apache (httpd.conf) and change
Listen 80 to Listen 8080. Then visit http://localhost:8080 instead.
Step 3 โ Install VS Code
- Download it free from code.visualstudio.com and install it.
- Optional but recommended: inside VS Code, open the Extensions panel (Ctrl+Shift+X) and install PHP Intelephense โ it underlines mistakes and autocompletes PHP for you, like a spelling checker for code.
Step 4 โ Your very first PHP file
Every PHP file you want to run must live inside XAMPP's special folder called
htdocs โ that is the "serving counter" of your kitchen.
- Open the folder
C:\xampp\htdocs. - Create a new folder inside it called
learn. All our course files will live here. - In VS Code, choose File โ Open Folder and open
C:\xampp\htdocs\learn. - Create a new file called
hello.phpand type this:
hello.php<?php
echo "Hello, world! My kitchen is working.";
?>
Save the file, then visit this address in your browser:
http://localhost/learn/hello.php
What just happened?
<?phptells the server: "PHP code starts here."echomeans "print this out" โ it sends text to the browser.- Every PHP instruction ends with a semicolon
;โ like a full stop at the end of a sentence. ?>means "PHP code ends here." (If a file contains only PHP, you may leave this closing tag out โ professionals usually do.)
Never open a PHP file by double-clicking it (which gives an address like
file:///C:/xampp/...). That skips the kitchen entirely and you'll just see raw code.
Always go through http://localhost/... so the server can cook the file first.
Mixing PHP and HTML
PHP's superpower is that it lives comfortably inside HTML. The server runs the PHP parts and leaves the HTML parts alone:
mixed.php<!DOCTYPE html>
<html>
<head><title>My First Page</title></head>
<body>
<h1>Welcome to my site</h1>
<p>Today's lucky number is: <?php echo rand(1, 100); ?></p>
</body>
</html>
Refresh the page โ the number changes every time! That's the difference between a
static page (always the same) and a dynamic page (cooked fresh on every visit).
rand(1, 100) is a built-in PHP function that picks a random number between 1 and 100.
Create a file called about.php in your learn folder. Make it show a
heading with your name, and a paragraph that uses echo to print your favourite food.
View it at http://localhost/learn/about.php.
Show solution
about.php<!DOCTYPE html>
<html>
<body>
<h1>Godfrey's Page</h1>
<p><?php echo "My favourite food is nshima with village chicken."; ?></p>
</body>
</html>
Where does PHP code actually run?
Where must your PHP files live so XAMPP can run them?
- PHP is a server-side language: it runs in the "kitchen" and sends finished HTML to the browser.
- XAMPP gives you Apache (server), PHP, and MySQL (database) in one install.
- Files must live in
htdocsand be opened viahttp://localhost/.... - PHP code lives between
<?phpand?>; statements end with;. echoprints output; PHP mixes freely with HTML to make pages dynamic.