Tutorials Logic, IN info@tutorialslogic.com

PHP Introduction: How Server-Side PHP Works

PHP Overview

PHP is an open-source, general-purpose scripting language designed especially for web development. In a web application, PHP normally runs on the server and creates the response that a browser receives.

After this lesson, you will be able to trace a PHP page from browser request to server execution, identify which code stays on the server, and run a small script through a web server or the command line.

PHP at a Glance

PHP stands for PHP: Hypertext Preprocessor. It can be embedded inside HTML, but it is also a complete language with variables, functions, classes, exceptions, file handling, database extensions, and command-line tools.

PHP is commonly used to read an HTTP request, apply application rules, communicate with a database or service, and produce HTML, JSON, a file, or another response.

  • Create dynamic pages whose content changes for each request or user.
  • Process forms, validate input, manage sessions, and send cookies.
  • Build websites, APIs, background jobs, and command-line tools.

Request and Response

When a browser requests a PHP page, the web server passes the file to the PHP runtime. PHP executes the server-side instructions and produces a response that the server returns to the browser.

The browser receives generated output, not the original PHP source. Private application logic therefore stays on the server, while secrets still belong in protected environment configuration rather than source files.

  • Request: the browser asks for a URL.
  • Execution: PHP runs the matching script.
  • Response: PHP produces a body and HTTP response data.
  • Rendering: the browser interprets returned HTML, CSS, and JavaScript.

PHP Files and Tags

PHP source files normally use the .php extension. The opening tag <?php starts PHP code, while <?= is a short form of <?php echo for inserting a value into HTML.

In a file that contains only PHP, omit the closing ?> tag to prevent accidental trailing whitespace from becoming response output.

PHP Inside HTML

The server evaluates the variable before returning the completed paragraph.

PHP Inside HTML
<?php
$completedLessons = 2;
?><h1>PHP Learning Dashboard</h1>
<p>Lessons completed: <?= $completedLessons ?></p>
Output
<h1>PHP Learning Dashboard</h1>
<p>Lessons completed: 2</p>

The browser receives the value 2 in ordinary HTML. It never sees the $completedLessons assignment.

PHP and Browser JavaScript

PHP and browser JavaScript usually cooperate. PHP prepares data and the initial response on the server; JavaScript updates the interface after the response reaches the browser.

Question PHP Browser JavaScript
Where does it run? On the server before the response is sent In the browser after code is downloaded
Typical work Application rules, authentication, database access, response creation Events, interface updates, browser APIs
Can visitors inspect it? Not in a correctly configured response Yes, downloaded code is inspectable

Run PHP Locally

For a web page, you need PHP, a web server, and a browser. XAMPP supplies Apache and PHP together: save a .php file under the configured web root, start Apache, and request the file through localhost.

For a command-line script, run php filename.php. On a default Windows XAMPP installation, use C:\xampp\php\php.exe filename.php when php is not available on PATH.

PHP Runtime Check

PHP Runtime Check
<?php
echo 'PHP is running.' . PHP_EOL;
Output
PHP is running.

PHP_EOL adds the correct line ending for the operating system.

PHP Setup Traps

  • Raw PHP appears in the browser: the request did not pass through a configured PHP runtime.
  • Double-clicking does nothing: a file:// URL cannot execute server-side PHP.
  • A short <? tag fails: use <?php for code and <?= for short output.
  • A database is not required to learn PHP syntax; add one only when the application needs persistent records.

Check the Request Flow

0 of 2 checked

Q1. Where does PHP normally execute for a web request?

Q2. What does the browser normally receive?

PHP Execution Boundary

  • Server-side code is not sent to the browser

    The browser receives PHP output, not the PHP source. Use view source and server logs to distinguish generated HTML problems from runtime failures.

Apply the request flow

Trace One Response

0 of 2 completed

  1. Run the PHP Inside HTML example, view the browser page source, and confirm that $completedLessons is absent. The response contains 2, not the variable assignment.
  2. Save the runtime check as runtime-check.php and execute it from a terminal. The terminal should print one line.
Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.