What PHP Is and How a Web Page Request Works

Level: Beginner
Time to read: 12 minutes
Track: AI with PHP · Foundations

If you want to add AI features to a website — a parent reminder, a marks summary, a small helper on a tuition-centre page — you still need to understand how PHP turns a request into a web page. That foundation is what this lesson builds. The AI call comes later; the request path is the same.

Imagine Priya, who runs Shree Vidya Tuition Centre in Pune. Parents ask the same questions every evening: how much was collected today, and did Ananya’s November fee of ₹1,500 already show up? Priya does not need a huge app yet. She needs a web page that can do a little work on the server and then show clear numbers in the browser. That small job is a perfect first look at PHP.

This lesson answers two questions: what PHP is, and what happens between a click and a finished page. You will leave with a short script and a mental picture of the request that every later AI or database lesson will reuse.

Key takeaways

  • PHP runs on the server and sends HTML (or other text) back to the browser; visitors do not see your source.
  • A page view is a request: browser → web server → PHP engine → response.
  • Any AI feature you add later is just another step inside that same request, or a new request of its own.
  • A short script can print a rupee total with echo and number_format().
  • Opening a .php file from disk is not the same as requesting it through a server — and secrets must never be echoed to the page.

What PHP is

PHP is a programming language built for the web. The letters now officially stand for “PHP: Hypertext Preprocessor,” but the useful idea is simpler: PHP is code that runs on a web server, prepares a result (usually HTML), and sends that result to the visitor’s browser.

A few properties matter from day one. PHP is server-side: the browser never sees your .php source, only the output, which is often ordinary HTML. It is widely used in India for WordPress sites, school and college portals, and billing pages. It talks easily to databases such as MySQL, which you will later use for marks and invoices. And you can start small — a few lines are enough to print today’s collection in rupees. You do not need a framework to learn the language.

PHP does not replace HTML, CSS, or JavaScript. HTML describes the page, CSS styles it, and JavaScript can run in the browser. PHP sits behind them and decides what the HTML should contain — whose name to show, which fee is due, whether a student passed Science.

How a web page request works

When you open a page such as https://shreevidya.example/today.php, a short conversation happens. It is tempting to think “the browser runs the PHP.” It does not. Here is the trip, step by step.

1. You ask for a URL. You type the address or click a link. The browser sends an HTTP request: a method (usually GET), a path such as /today.php, and headers (what it can accept, a cookie if you have visited before).

2. The request reaches the web server. A program such as Apache, Nginx, LiteSpeed, or PHP’s own tiny built-in server while you learn, receives it. If the path is a static file such as logo.png, the server can often send that file back immediately.

3. The server hands .php files to the PHP engine. The engine receives the script plus request details: the path, any query string such as ?roll=R12, form fields, and headers.

4. PHP runs from top to bottom. It can set variables, add amounts (₹2,450 plus ₹1,500), make decisions, or later read a database. Anything you echo becomes part of the response body. If you echo HTML, you are building the page as text.

5. PHP finishes and the server sends the response. The body is usually an HTML document, with a status code such as 200 (OK). Your original PHP statements are not in that body.

6. The browser draws the page. It parses the HTML, requests CSS, images, and JavaScript, and shows the result. If JavaScript later calls a PHP page again, that is a new request and the same six steps repeat.

Browser                    Web server                 PHP engine
   |                            |                          |
   |  GET /today.php            |                          |
   |--------------------------> |                          |
   |                            |  run today.php           |
   |                            |------------------------->|
   |                            |  HTML, e.g. "₹2,450"     |
   |                            |<-------------------------|
   |  HTML response             |                          |
   |<-------------------------- |                          |
   |  (then CSS, images...)     |                          |

Keep this picture. When a page is blank, the failure may be in PHP (the script crashed before it echoed anything), in the server (it never called PHP), or in the browser (it received HTML but CSS hid it). Knowing who does what saves a lot of guessing.

A first script: today’s fee summary

Here is a tiny program Priya can run. It does not talk to a database yet. It just stores a few values and prints a short summary. Save it as today.php.

<?php
$centreName = "Shree Vidya Tuition Centre";
$city = "Pune";
$todayCollection = 2450; // rupees collected today

echo "<h1>Daily fee summary</h1>";
echo "<p>" . $centreName . " in " . $city . "</p>";
echo "<p>Today's collection: ₹" . number_format($todayCollection) . "</p>";

A few things to notice, even before the syntax lesson. The file starts with <?php, which tells the engine to run PHP. Names such as $centreName are variables and need the $ prefix. 2450 is a number; number_format() turns it into 2,450, which is easier to read in rupees. echo sends text to the response, and because that text is HTML, the browser can show a heading and paragraphs.

If PHP is installed, you can run the same logic in the terminal without a browser:

<?php
$centreName = "Shree Vidya Tuition Centre";
$city = "Pune";
$todayCollection = 2450;

echo "Daily fee summary\n";
echo $centreName . " in " . $city . "\n";
echo "Today's collection: ₹" . number_format($todayCollection) . "\n";

On this lesson’s test machine, PHP 8.4 printed:

Daily fee summary
Shree Vidya Tuition Centre in Pune
Today's collection: ₹2,450

In a browser the first version wraps the same words in HTML tags. The request path is identical: PHP runs on the server (or in the terminal), then you see only the output.

Try this

Change $todayCollection to 2450 + 1500 to include Ananya Sharma’s November fee, save the file, and refresh. You should see ₹3,950. Then add a second variable, $receiptCount, and echo a line such as “Receipts issued today: 11.” Each refresh is a brand-new request. PHP does not remember the old numbers unless you store them somewhere (a database or a session), which comes later in the track.

HTML and PHP in the same file

You do not have to echo every tag. A common style is to write HTML as usual and open PHP only where you need a calculated value. The file still uses a .php extension so the server will run those bits.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Daily fee summary</title>
</head>
<body>
  <h1>Daily fee summary</h1>
  <p>Shree Vidya Tuition Centre, Pune</p>
  <p>Today's collection: ₹<?php echo number_format(2450); ?></p>
</body>
</html>

The browser receives a finished paragraph, “Today’s collection: ₹2,450.” It has no idea that number_format ran on the server. That split is the point: you write instructions, the visitor receives a document.

What this mental model prevents

Beginners often hit the same traps. If you remember the six steps, they make sense. Opening a path such as file:///home/priya/today.php is not a request to a web server, so PHP never runs — use a local server (for example php -S localhost:8000) and visit http://localhost:8000/today.php. If you change a file but the page stays old, the browser may have cached the HTML, or you edited a different copy than the server is reading. Visitors cannot download your PHP source from a correctly configured server; they only see the HTML. And anything you echo is visible, so fee totals are fine, but a database password or API key must stay on the server, ideally in an environment variable.

What you learned

  • PHP is a server-side language. It prepares a response, usually HTML, and the browser only sees that output.
  • A page view is a request: browser to web server, server to the PHP engine, PHP runs the script, then HTML travels back.
  • A short script can store a centre name, a city, and a rupee amount, then print a readable summary with echo and number_format().
  • You can mix HTML with small <?php ... ?> blocks when you only need one calculated value.
  • Opening a .php file from disk is not the same as requesting it through a server.

Quick quiz

  1. 1. Where does a PHP script run when someone visits a page?

    • A) In the visitor’s browser, like JavaScript
    • B) On the web server, before the HTML is sent back
    • C) Only on your laptop, never on a host
    Show answer

    B. PHP is server-side. The browser receives the output (usually HTML), not the .php source.

  2. 2. You double-click today.php and the browser shows a file:/// address. Why is the fee total missing?

    • A) number_format() does not work with rupees
    • B) The PHP engine never ran, because there was no request to a web server
    • C) The file must be renamed to .html
    Show answer

    B. A file:/// path is not an HTTP request. Use a local server such as php -S localhost:8000 and open the http URL.

  3. 3. When you later call an AI API from this page, who should supply the rupee balance: the model, or your PHP?

    • A) The model, because it is good at arithmetic
    • B) Your PHP (or database), then you put that amount in the prompt
    • C) It does not matter
    Show answer

    B. Official amounts belong in your data. The model should phrase the message, not invent the figure. That habit starts with understanding that PHP already owns the request.

  4. 4. Why must you not echo an API key or database password into the page?

    • A) Echo only works with numbers
    • B) Anything you echo becomes visible in the HTML the browser receives
    • C) PHP will automatically hide secrets
    Show answer

    B. The response body is public to the visitor. Keep secrets on the server, preferably in environment variables.

Next lesson

Next you will look at PHP syntax — the punctuation that keeps a file valid: tags, statements, comments, and how a line ends. After that the AI with PHP foundations continue through variables, output, and data types, still using examples from tuition records and rupee amounts rather than made-up placeholders.

Those same building blocks — a script that can print a rupee total as part of a web page — are what you will later hand to a model as facts, not as something the model should guess.