40% off everything — use code SAVE40 at checkout · ends in …
Skip to content
phpguru.store
Lesson 1 of 12 4 min read Free preview

How PHP works: the request, the server and your first script

What happens between a browser asking for a page and PHP answering, where your code runs, how to get PHP on your own machine in ten minutes, and the one habit that makes everything after this easier.

What PHP is, in one paragraph

PHP is a language that runs on the server. A browser asks your server for contact.php; the server hands that file to PHP; PHP runs the code inside it and produces plain HTML; the server sends that HTML back. The browser never sees a line of PHP — only the result. That single fact explains most of PHP's behaviour: it can read databases and files and secrets because it runs on your machine, not the visitor's, and it can't react to a mouse click without a new request, because by the time the click happens PHP has already finished.

Almost every product on this site — hotel management, school management, invoicing — is exactly this loop repeated: request comes in, PHP reads or writes the database, HTML goes out.

Where PHP runs

You have two options, and you'll end up using both:

  • On your own computer, for building and testing. The easiest way is XAMPP (Windows/macOS/Linux) or Laragon (Windows): they install PHP, MySQL and the Apache web server together. After installing, anything you put in the htdocs folder is served at http://localhost/.
  • On shared hosting, for the real thing. Hosts like Hostinger already have PHP and MySQL set up; you upload files with their File Manager or FTP and they are live at your domain. Lesson 12 walks through this.

Install XAMPP now if you haven't. Start Apache from its control panel, open http://localhost/ in a browser, and you should see the XAMPP welcome page. That's a working PHP server.

Your first script

Inside htdocs, create a folder called learn, and inside it a file hello.php:

<?php
$name = "world";
echo "<h1>Hello, " . $name . "!</h1>";
echo "<p>Today is " . date("l, j F Y") . ".</p>";

Open http://localhost/learn/hello.php. You should see a heading and today's date. Three things just happened:

  1. <?php told the server "PHP starts here". Everything after it is code, not text.
  2. $name = "world"; stored a value in a variable. Every statement ends with a semicolon — forgetting one is the most common beginner error, and PHP's message for it ("syntax error, unexpected…") points at the line *after* the missing semicolon.
  3. echo sent text to the browser. The dot . joins strings together. date() is one of thousands of built-in functions; you'll meet the useful ones as you go.

Now do the thing that separates people who learn PHP from people who read about it: change the code and reload. Change "world" to your name. Remove the semicolon on line 2 and read the error. Put the date() call inside the <h1>. Every lesson in this course expects you to do this — the reading is the map, the typing is the territory.

Mixing PHP and HTML

A PHP file is really an HTML file that can switch into code. This is the same page, written the way real templates are written:

<?php
$name = "world";
$items = ["Hotel", "School", "Pharmacy"];
?>
<!DOCTYPE html>
<html>
<body>
  <h1>Hello, <?= $name ?>!</h1>
  <ul>
    <?php foreach ($items as $item): ?>
      <li><?= $item ?> management system</li>
    <?php endforeach; ?>
  </ul>
</body>
</html>

<?= ... ?> is shorthand for <?php echo ...; ?>. The foreach (...): ... endforeach; form exists precisely so PHP and HTML can interleave without a wall of braces. You'll use both styles constantly: logic at the top of the file in plain PHP, output at the bottom mixed with HTML.

One rule from day one: anything a visitor typed must be escaped before you echo it. If $name came from a form and someone typed <script>alert(1)</script>, <?= $name ?> would run their script in every other visitor's browser. The fix is a habit, not a feature:

<h1>Hello, <?= htmlspecialchars($name) ?>!</h1>

Lesson 7 explains this properly. For now, notice that the products sold on this site wrap every output in a helper that does exactly this — it's the difference between a script and a product.

Seeing errors

While learning, you want PHP to shout. Put this at the top of every file you experiment with:

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);

On a live site you turn display_errors off and log instead (Lesson 11), because an error message can leak file paths and database names to strangers. On your laptop, leave it on: a visible error is a faster teacher than a blank page.

What "PHP 8" means for you

The code in this course is PHP 8 (8.1 and later). Hosts run 8.1–8.3 today; the free scripts floating around the internet are often written for PHP 5 and break on modern servers with errors like "Call to undefined function mysql_connect()". When you see that error on someone else's code, it isn't your fault — that function was removed in PHP 7, a decade ago. Everything you learn here uses the modern replacements.

Try it

  • Make hello.php print your name, the current year, and a list of three things you want to build.
  • Deliberately break the file three different ways (missing semicolon, missing quote, misspelled echo) and read each error message before fixing it. You're learning the vocabulary of the messages you'll see for years.
  • Look at the address bar: hello.php is the *file*, http://localhost/learn/ is the *folder*. Rename the file and watch the URL change. That mapping of folders to URLs is how most PHP sites are organised.

That was lesson 1. The other 11 are one payment away.

Next up: Variables, types and the rules of the language. Buy once, read every lesson online with your progress saved, and keep the whole course as a Word file.

Get the course · ₹999$29