<?php OOP: Zero to Hero Chapter 01 of 20

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.

๐Ÿณ
Real-world analogy

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

ToolWhat it isWhy we need it
XAMPPA 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 CodeA free code editor from MicrosoftWhere we will actually type our PHP code
A browserChrome, Firefox or EdgeTo view the pages our PHP produces

Step 1 โ€” Install XAMPP

  1. Go to apachefriends.org and download XAMPP for your operating system (Windows, Linux or macOS).
  2. Run the installer. You can accept all the default options โ€” just keep clicking Next.
  3. On Windows, XAMPP installs itself into C:\xampp. Remember that folder โ€” we will live there.

Step 2 โ€” Start Apache and MySQL

  1. Open the XAMPP Control Panel (search for "XAMPP" in your Start menu).
  2. Click Start next to Apache. Its name should turn green.
  3. Click Start next to MySQL. It should also turn green.
  4. Open your browser and visit http://localhost. If you see the XAMPP welcome page โ€” congratulations, your kitchen is open for business! ๐ŸŽ‰
If Apache refuses to start

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

  1. Download it free from code.visualstudio.com and install it.
  2. 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.

  1. Open the folder C:\xampp\htdocs.
  2. Create a new folder inside it called learn. All our course files will live here.
  3. In VS Code, choose File โ†’ Open Folder and open C:\xampp\htdocs\learn.
  4. Create a new file called hello.php and 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
Hello, world! My kitchen is working.

What just happened?

Very important

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>
Welcome to my site Today's lucky number is: 47

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.

โœ๏ธ Try it yourself

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>
Quick quiz

Where does PHP code actually run?

Quick quiz

Where must your PHP files live so XAMPP can run them?

๐Ÿ”‘ Key points
  • 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 htdocs and be opened via http://localhost/....
  • PHP code lives between <?php and ?>; statements end with ;.
  • echo prints output; PHP mixes freely with HTML to make pages dynamic.