beginner15 min

Introduction to PHP and the Request-Response Model

What PHP is, how a .php file handles a web request, and embedding PHP in HTML.

What you'll learn

  • Explain what kind of language PHP is and how it fits into the request-response cycle
  • Identify the `<?php ?>` tags that switch a file between HTML and PHP
  • Predict the output of a small PHP script that mixes `echo` statements

Explanation

PHP is a server-side scripting language: code runs on the server, per request, and the result (typically HTML) is what actually reaches the browser -- the browser never sees your PHP source. A visitor's request for page.php causes the server to run page.php from top to bottom, and whatever it echos (or otherwise outputs) becomes the HTTP response body. This is different from a language like JavaScript running in the browser: PHP's whole job happens before the response is ever sent.

A .php file can freely mix literal HTML with PHP logic. Anything outside <?php ... ?> tags is sent to the browser exactly as written; anything inside those tags is executed as PHP code. This lets a single file open in plain HTML, drop into <?php ?> to compute something dynamic, and drop back out to HTML -- a pattern that predates (and still coexists with) more structured templating approaches.

Inside PHP tags, echo is the workhorse statement for producing output: echo "Hello!"; sends the literal text Hello! to the response. Unlike some languages' print functions, echo does not add a trailing newline automatically -- if you want one, you write "\n" explicitly in the string.

There's no separate "build" step a developer runs before a request can be served -- the PHP engine parses and executes the file directly (internally compiling to an intermediate bytecode for the request, but that's invisible to you as a developer; you just save the file and reload the page).

Guided lab

Predict: A minimal PHP script

PHPNot executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this script and predict exactly what it sends to the browser.

<?php
$name = "World";
echo "Hello, $name!\n";
echo "2 + 2 = " . (2 + 2) . "\n";

Stuck? Get a hint.

Common mistakes

  • Forgetting to open `<?php` before writing PHP code in a file that also contains HTML -- code outside the tags is sent to the browser as literal text, not executed.
  • Assuming `echo` adds a newline automatically the way some other languages' print functions do -- PHP requires an explicit `\n` in the string if you want one.
  • Thinking a `.php` file needs an explicit compile/build command before it can serve a request -- the PHP engine parses and runs it directly when requested.

Knowledge check

Knowledge check

1. When a browser requests a `.php` file, where does the PHP code actually run?
2. What happens to text written outside `<?php ... ?>` tags in a .php file?
3. Does a PHP developer need to run a separate build/compile command before a request can be served?

Takeaway

PHP code runs on the server per request, freely mixed with HTML via `<?php ?>` tags; `echo` sends output but never adds a newline for you.

Summary

PHP is a server-side scripting language executed per request; `<?php ?>` tags switch between literal HTML and executed code, and `echo` produces output with no automatic newline.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.