Explain it simply
PHP in plain words
PHP stands for 'PHP: Hypertext Preprocessor'. It is an open-source language made for the web: you write ordinary HTML and drop small PHP snippets in wherever something has to be calculated. The server runs each snippet and replaces it with the text it produced.
Think of a mail-merge letter. A template that says 'Dear {name}' is filled in once per person. The PHP file is the template and the server is the one filling it in, while the recipient only ever sees the finished letter.
Problem → Why → Solution
The problem: the PHP is there, but nothing shows on the page
This is something beginners run into constantly. The code is written correctly, but the file was saved as `hello.html`, or an on-disk file was opened straight in the browser. The page shows the heading, yet the spot where the greeting should be is empty, and no error message appears anywhere.
The web server looks at the file extension (and its own configuration) to decide which files go to PHP. A `.html` file is not sent there, so the server delivers it untouched, and the browser treats `<?php ... ?>` as an unknown tag, much like a comment, and skips it.
<!-- hello.html: saved with the wrong extension -->
<h1>My first PHP page</h1>
<?php echo "Hello World!"; ?>
<!-- the server sends this file as it is; the greeting never appears -->Why there is no error
The PHP engine never saw the file, so it has nothing to complain about, and as HTML there is nothing wrong either. That is why you need to understand from the start which files PHP handles and how it was started.
Core concept: language, engine and SAPI are three different things
The word PHP actually covers three layers, and keeping them apart makes later problems much easier to diagnose. The language is the syntax and rules you write: variables, functions, classes. The engine is the program that reads your script, compiles it and executes it, together with the standard library and extensions such as mbstring and PDO. The SAPI (Server API) is the doorway through which the engine is started, and it decides where input comes from and where output goes.
| Piece | What it does | Where you meet it |
|---|---|---|
| Language | The syntax and rules you write | Every `.php` file |
| Engine and extensions | Runs the script and supplies functions and classes such as `strlen()` and `PDO` | `php -v`, `php -m` |
| SAPI | Starts the engine and connects it to a terminal or a web server | `cli`, `fpm-fcgi`, `apache2handler`, `cli-server` |
| php.ini | Settings read at start-up | `php --ini` |
A `.php` file starts out in HTML mode. Anything outside `<?php ... ?>` is copied to the output unchanged, and anything inside is executed as PHP. The shortcut `<?= expression ?>` means `<?php echo expression; ?>`. This is why PHP works both as a general language and as a template language: you write a page the way you would write HTML, and drop in PHP exactly where the page needs a computed value.
Configuration travels with the SAPI. Each start-up reads a php.ini file (`php --ini` shows which one), and the terminal and the web server often read different files, or even use different PHP installations. A script that works with `php script.php` but fails in the browser is usually a configuration difference, not a language bug.
- Outside the tags: copied as text
- Inside <?php ?>: PHP code runs
- The value of <?= ?> is printed in place
- Browser gets the combined output
PHP never alters the text outside the tags; it only runs the `<?php ... ?>` parts and replaces them with their output.
General pattern: PHP inside HTML
<?php
// PHP mode: every statement ends with a semicolon
$variable = expression;
?>
<tag>plain HTML is copied exactly as written</tag>
<tag><?= $variable ?></tag>Between `<?php` and `?>` you are in PHP mode, and every statement ends with a semicolon. After `?>` you are back in HTML mode. `<?= $variable ?>` prints the variable at that exact spot.
Example: a page that mixes HTML and PHP
Save the code below as `page.php`. Running `php page.php` in a terminal lets you see exactly the HTML a server would send to the browser. Notice that the PHP parts are gone and the text they produced has taken their place. The second `<p>` reports which SAPI started the script.
<?php
declare(strict_types=1);
$title = "My first PHP page";
$sapi = PHP_SAPI;
?>
<!DOCTYPE html>
<html>
<body>
<h1><?= $title ?></h1>
<?php
echo "<p>Hello World!</p>\n";
echo "<p>This HTML was produced by the {$sapi} SAPI.</p>\n";
?>
</body>
</html>Code breakdown
- PHP mode before the HTML
$title = "My first PHP page";The `<?php ... ?>` block at the top prepares variables. It prints nothing, so it leaves no trace in the output.
- HTML mode
<!DOCTYPE html>From `<!DOCTYPE html>` down to `<body>`, PHP copies every line to the output without touching it.
- Short echo tag
<h1><?= $title ?></h1>`<?= $title ?>` prints the value of `$title` inside the `<h1>`. No semicolon is needed before the closing `?>`.
- echo inside a PHP block
echo "<p>Hello World!</p>\n";Inside a PHP block you can also produce HTML with `echo`. The `\n` adds a new line so the output stays readable.
- A variable inside a string
echo "<p>This HTML was produced by the {$sapi} SAPI.</p>\n";`{$sapi}` places the value of `$sapi` (`cli`) into the string. Opened through a web server in a browser, a different value would appear.
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<p>Hello World!</p>
<p>This HTML was produced by the cli SAPI.</p>
</body>
</html>The tag forms and the closing tag
| Form | Meaning | Advice |
|---|---|---|
| `<?php ... ?>` | The standard tag; works everywhere | Always use it |
| `<?= expr ?>` | Shorthand for `<?php echo expr; ?>` | Great inside templates |
| `<? ... ?>` | Short open tag; depends on the `short_open_tag` setting | Avoid it |
In a file that contains only PHP code it is normal to leave out the closing `?>`. A stray space or newline after it would become part of the output and can cause trouble later, when headers are sent. Also remember that PHP itself removes a single newline directly after `?>`.
<?php
$items = ["tea", "coffee"];
?>
<ul>
<?php foreach ($items as $item): ?>
<li><?= htmlspecialchars($item) ?></li>
<?php endforeach; ?>
</ul>The loop (`foreach`) is covered properly in later lessons; for now just notice how HTML and PHP take turns. `htmlspecialchars()` turns characters such as `<` and `&` into harmless HTML text, so text typed by a user can be shown safely. The output is a `<ul>` with two `<li>` lines.
PHP 8.x and this course
PHP started in the mid-1990s as small tools for making web pages and has changed a great deal since. PHP 8.0 brought named arguments, `match` expressions, the nullsafe operator (`?->`), union types and attributes, and later 8.x releases added enums and readonly properties. This course targets PHP 8.x, so it uses things old tutorials skip, such as strict types and typed parameters, from the beginning. Check your own version with `php -v`.
In real projects
Where the three layers matter in real projects
Most shared hosting pairs Apache with PHP, while modern deployments favour PHP-FPM behind Nginx, and Docker images carry tags such as `php:8.x-fpm`. A WordPress theme is exactly the kind of file this lesson describes, HTML mixed with PHP, where snippets such as `<?php the_title(); ?>` fill in the page's values.
The CLI-versus-web difference matters in practice. Cron jobs, queue workers and Composer scripts run through the CLI while web requests run through FPM, so settings such as memory_limit can differ. The default `max_execution_time` is 0 (unlimited) in the CLI, so a script that takes five minutes may be fine under cron and still be cut off on the web. Knowing which SAPI a problem belongs to saves hours of guessing.
Common mistakes
Common beginner mistakes
1. The wrong extension, or no server at all
PHP code inside a `.html` file is never sent to PHP, and a `.php` file opened straight from disk is not run either. The fix is to save the file as `.php` and open it through a server (`http://localhost:8000/page.php`) or run `php page.php` in a terminal.
2. Assuming the terminal and the browser share one PHP
`php -v` may say 8.3 while the page in your browser reports 7.4, because a bundle such as XAMPP ships its own PHP and its own php.ini. To learn which version the browser uses, run `echo PHP_VERSION;` through that same SAPI.
3. Thinking PHP hides the output too
PHP hides the code, not the output it produces. Even inside an HTML comment such as `<!-- <?= $apiKey ?> -->` the key is sent to the browser and visible in View Source. Never put secret values into the output.
Mini challenge
Mini challenge: a profile card template
Write a `card.php` template that builds an HTML card from variables holding a name, a role and a list of skills. Use `<?= ?>` to place the values into the HTML.
- In a `<?php ... ?>` block at the top, define `$name`, `$role` and a `$skills` array
- Store the number of skills, from `count()`, in `$skillCount`
- The card needs an `<h2>`, a `<p>` with the role and skill count, and a `<p>` listing the skills
- Pass every text value through `htmlspecialchars()`
<?php
declare(strict_types=1);
// TODO: define $name, $role and a $skills array
// TODO: store count($skills) in $skillCount
?>
<!-- TODO: a <section> holding an <h2> with the name -->
<!-- TODO: a <p> with the role and the skill count, built with <?= ?> -->
<!-- TODO: a <p> listing the skills, joined with implode() -->Expected: `php card.php` prints a `<section class="card">` containing the name, "PHP learner with 3 skills" and "Skills: HTML, PHP basics, SQL", one per line.
Show hint
`implode(", ", $skills)` turns the array into one comma-separated string. `<?= htmlspecialchars($name) ?>` escapes the value and prints it in one step.
Show solution
<?php
declare(strict_types=1);
$name = "Aye Aye";
$role = "PHP learner";
$skills = ["HTML", "PHP basics", "SQL"];
$skillCount = count($skills);
?>
<section class="card">
<h2><?= htmlspecialchars($name) ?></h2>
<p><?= htmlspecialchars($role) ?> with <?= $skillCount ?> skills</p>
<p>Skills: <?= htmlspecialchars(implode(", ", $skills)) ?></p>
</section>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- PHP means three layers: the language, the engine and the SAPI, and the SAPI decides how the engine is started
- In a `.php` file, text outside `<?php ... ?>` is copied and the code inside runs and is replaced by its output
- `<?= expr ?>` is shorthand for `<?php echo expr; ?>` and works everywhere
- The terminal and the web server may use different php.ini files, so check configuration differences first
Migrating to PHP 8.5 — PHP Documentation Group