What you'll learn
PHP is a server-side programming language built for the web. In this lesson you'll see exactly where it runs, set up a useful development loop, and create a real dynamic page.
By the end, you'll be able to:
- Explain the difference between browser code and server-side PHP
- Trace an HTTP request through a web server and PHP
- Run PHP from both the command line and a local web server
- Use PHP tags, statements, variables,
echo, and comments - Read an error message and use it to find the failing line
Where PHP runs
HTML, CSS, and JavaScript are sent to the browser. PHP is different: the server executes the PHP source first and sends only the resulting response—usually HTML or JSON. Visitors never need PHP installed, and they should never receive your source code.
Browser
Requests /hello.php
Web server
Routes the request to PHP
PHP
Executes code and builds output
Browser
Receives an HTML response
Key idea
Set up a development environment
Install a supported PHP 8 release using the package manager or installer for your operating system. A beginner-friendly all-in-one environment is fine, but learning the two commands below keeps your workflow portable.
php --version
php --iniCreate a folder for the course, open a terminal there, then start PHP's development server:
mkdir php-course
cd php-course
php -S localhost:8000Development only
Your first PHP script
Save this as hello.php. Run it with php hello.php in the terminal, or visit http://localhost:8000/hello.php while the local server is running.
<?php
$student = "Maya";
$topic = "PHP";
echo "Hello, {$student}!";
echo "<p>Welcome to {$topic}.</p>";PHP variables begin with $. Statements normally end with a semicolon. Double-quoted strings can interpolate variables, and echo writes values into the response.
PHP tags and mixed templates
A .php file can switch between PHP and ordinary HTML. The short echo tag<?= ... ?> is a concise, always-available way to output a value inside markup.
<?php
// A small server-rendered profile
$name = "Omar";
$goal = "Build secure web apps";
$lessonsCompleted = 1;
?>
<h1><?= htmlspecialchars($name) ?></h1>
<p>Goal: <?= htmlspecialchars($goal) ?></p>
<p>Lessons completed: <?= $lessonsCompleted ?></p>Escape untrusted text
htmlspecialchars() prevents data from becoming executable markup.PHP supports // and # line comments plus /* ... */ block comments. Comments should explain intent or constraints—not repeat obvious code.
Errors and a professional feedback loop
During development, keep error reporting visible, read the first useful message, note its file and line, and reduce the problem to the smallest failing expression. In production, errors belong in logs rather than public responses.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo "Development errors are visible.";Note
Practice challenge
Create about.php that stores your name, learning goal, and current module in variables, then renders them inside semantic HTML. Add one PHP comment explaining why you escape text.
Recap & quick check
Key takeaways
- PHP executes on the server; clients receive its output, not its source code.
- Use the CLI for scripts and php -S localhost:8000 for local web lessons.
- PHP code starts with <?php; variables use $, statements usually end with semicolons, and echo produces output.
- A PHP file can mix server-side logic with HTML, using <?= ... ?> for concise output.
- Development errors should be visible locally and logged—but hidden from visitors—in production.
Quick check
1. Where does PHP normally execute in a web application?
2. Which command starts PHP's local development server on port 8000?
3. What is the safest default for displaying detailed errors in production?