What PHP Actually Is (And Why It Exists)
Before writing a single line of code, it's worth slowing down and understanding what PHP actually is — not just as a definition to memorize, but as a tool built to solve a real problem.
PHP stands for "PHP: Hypertext Preprocessor" — yes, it's a recursive acronym, where the P in PHP stands for PHP itself. That's not a typo, it's an old inside joke from the GNU project tradition. Jokes aside, PHP is a general-purpose scripting language built specifically to be embedded into HTML and generate dynamic content on a server.
Why Was PHP Created?
Rewind to the early-to-mid 1990s. Web pages back then were static — plain HTML files that never changed unless someone physically edited them. There was no way for a page to say "only show this if the user is logged in," or "display today's date," or "pull this data from a database."
Rasmus Lerdorf created PHP in 1994 (originally called "Personal Home Page Tools") to solve exactly this problem — letting a server generate HTML dynamically, based on logic, data, and user input, before sending the final result to the browser.
A Quick, Practical Look at PHP's Evolution
You don't need a full history lesson, but knowing the rough version timeline actually matters — because a lot of PHP tutorials floating around online are wildly outdated:
- PHP 5 (2004–2015): Introduced real object-oriented programming. Still lingering in old legacy systems — avoid learning from PHP 5-era tutorials.
- PHP 7 (2015–2019): A massive performance rewrite. Added scalar type declarations, the spaceship operator, the null coalescing operator, and return types.
- PHP 8.x (2020–present): Added
match, named arguments, constructor property promotion, enums, readonly properties, the nullsafe operator (?->), a JIT compiler, union types, and first-class callable syntax.
We'll be working with modern PHP (8.x) throughout this course. Whenever something changed meaningfully between versions, I'll flag it clearly like this:
🔄 Version note: a short explanation of what changed and why it matters.
Where PHP Genuinely Shines
- An extremely mature ecosystem — Composer, Laravel, Symfony, WordPress. PHP still powers a massive share of the web.
- Fast for web workloads, especially with OPcache and the JIT compiler.
- Very easy to deploy — almost every shared host supports it out of the box.
- A huge standard library of built-in functions for strings, arrays, files, and networking, without needing external packages for the basics.
- Strong typing tools available whenever you want them —
declare(strict_types=1), typed properties, union types.
And Where It Falls Short
- Inconsistent function naming across the years —
str_replacevsstrposvsarray_push. That's a real wart, not a myth. - It's easy to write insecure or messy code if you don't know what you're doing — which is exactly why this course insists on modern, secure patterns from day one, instead of "quick and dirty" beginner habits.
- Not ideal for CPU-heavy, long-running processes (like real-time systems) compared to languages built for that purpose — though this is a niche concern for where you're at right now.
PHP CLI vs PHP Web Execution
This is a genuine distinction you need to understand — not just trivia.
| Mode | How It Runs | Typical Use |
|---|---|---|
| PHP CLI (Command Line Interface) | You run php script.php directly in a terminal. No browser, no web server involved. |
Scripts, automation, cron jobs, CLI tools — like the Expense Tracker project we'll build later. |
| PHP Web Execution | A web server (Apache/Nginx) or PHP's built-in dev server receives an HTTP request and executes a .php file, sending output back as an HTTP response. |
Websites, web apps. |
It's the same PHP language and the same engine underneath — but the entry point differs, and so do the available superglobals. For instance, $_GET/$_POST only make real sense in web mode; in CLI mode they'll simply feel empty or undefined.
PHP Extensions and php.ini
PHP's core is deliberately kept small. A lot of functionality — working with images, connecting to MySQL, handling JSON, and more — is provided by extensions: optional modules compiled into or loaded by the PHP engine.
php.ini is PHP's main configuration file, and it controls things like:
error_reporting— what kinds of errors PHP reports (this becomes crucial later, in Phase 9.13).upload_max_filesize— the maximum file upload size.memory_limit— how much RAM a single script is allowed to use.display_errors— whether errors are shown directly on screen (this should be off in production, and on in development — we'll drill this later).
As a beginner you won't be editing this file heavily, but you absolutely need to know it exists — and know where PHP is reading its settings from. That's exactly what php --ini tells you.
Terminal Setup — Let's Actually Do This
Open your terminal and run:
php -v
What this does: -v is a flag meaning "version." This asks the PHP binary on your system to report its own version number and build info, then exit. It confirms PHP is installed and tells you exactly which version you're working with — important, because we're teaching PHP 8.x syntax, and if you see PHP 7.x or 5.x output here, some of the features I teach simply won't work on your machine.
php --ini
What this does: Shows you the path to the php.ini file currently being used, along with any additional .ini files loaded. This matters later when we adjust error display settings.
php -S localhost:8000
What this does, broken down:
-Sstarts PHP's built-in development web server (available since PHP 5.4). This is not meant for production — it's a lightweight server for local development only.localhostmeans "this same machine" (the loopback address127.0.0.1).8000is the port number — think of it as a specific "channel" on your machine that the server listens on. You'll then visithttp://localhost:8000in your browser to see PHP files in your current directory being served.
This one command replaces the need to install Apache, Nginx, or XAMPP just to start learning — a deliberately modern, PHP-friendly workflow.
Practical Exercise
- Run
php -vand tell me the exact version output. - Run
php --iniand tell me the path it reports for the loadedphp.ini. - Create a folder, and inside it create a file called
hello.phpcontaining just:<?php echo "PHP is working"; - From inside that folder, run
php -S localhost:8000, then visithttp://localhost:8000/hello.phpin your browser. Tell me what you see. - Now run the same file directly via CLI:
php hello.php. Tell me what you see, and how (if at all) it differs from the browser output.
PHP Syntax — The Grammar Rules Behind the Language
What It Is
PHP syntax is the set of grammar rules that define how you write valid PHP — how to open and close PHP code blocks, terminate statements, write comments, and mix PHP with HTML.
Why It Exists
The parser needs unambiguous rules to know exactly where PHP code begins, where it ends, and where one instruction stops and the next begins. Without these rules, the engine couldn't reliably translate your code into opcodes.
PHP Tags — How It Works
<?php
// PHP code goes here
?>
<?phptells the PHP engine: "everything from here onward is PHP code, not literal text or HTML to output as-is."?>tells the engine: "PHP code ends here — go back to treating everything as plain text or HTML."
🔄 Version/practice note: There used to be a short tag<? ?>and an echo shorthand<?= ?>. The short open tag<?is disabled by default and considered bad practice, since it's unreliable across server configurations. The echo shorthand<?= $variable ?>, on the other hand, is still valid, modern, and genuinely encouraged for embedding output directly inside HTML templates — you'll see it used properly once we reach Phase 9.
A closing tag rule you must know: if a .php file contains only PHP code with no trailing HTML, the modern best practice is to omit the closing ?> tag entirely.
Why? Because if there's any whitespace or newline after ?>, PHP can accidentally output that whitespace as part of the HTTP response — which causes a notorious bug category known as "headers already sent" errors (this becomes relevant once we touch sessions and cookies, which require headers to be sent before any output).
So this is the modern-correct pattern for pure-PHP files:
<?php
echo "Hello";
// no closing tag here — the file just ends
Statements and Semicolons
A statement is one complete instruction. In PHP, statements are terminated with a semicolon ; — much like a period ends a sentence in English.
echo "Hello";
$x = 5;
Each line above is a separate, complete statement. The semicolon tells the parser, "this instruction is finished." Forgetting it is one of the most common beginner syntax errors, and it produces a parse error — meaning the parser couldn't even build a valid structure from your code, so nothing runs at all. (We'll formally distinguish parse errors from other error types in section 9.13.)
Comments
Comments are text the PHP engine ignores entirely — they exist for humans, not for the machine.
// This is a single-line comment
# This is also a single-line comment (less common style)
/*
This is a
multi-line comment
*/
When to use which: // is the dominant modern convention for single-line comments. # exists but is rarely used stylistically in modern codebases — it shows up more in shell scripts and config-style files. /* */ is best used for longer explanatory blocks, or for temporarily disabling a chunk of code.
PHP Embedded in HTML — What Actually Happens Under the Hood
This part is genuinely important to understand at the execution level, not just syntactically.
<html>
<body>
<h1>Welcome</h1>
<?php
echo "<p>Today is a good day to learn PHP.</p>";
?>
<footer>Thanks for visiting</footer>
</body>
</html>
Here's the execution trace, step by step:
- The PHP engine starts reading the file top to bottom.
- Everything before
<?phpis not PHP — it's treated as literal text and sent straight to the output buffer as-is (<html><body><h1>Welcome</h1>). <?phpis encountered → the engine switches into "PHP parsing mode."echo "<p>...</p>";is executed → the string inside the quotes is added to the output buffer. Note: this string happens to contain HTML, but to PHP it's just a string value — PHP has no special awareness that<p>means anything.?>is encountered → the engine switches back into "literal text mode."- Everything after
?>(<footer>...) is once again literal text, appended as-is to the output buffer. - The script finishes → the entire output buffer is sent as the HTTP response body → the browser receives one continuous HTML document and renders it.
Key insight: PHP doesn't "understand" HTML. It's simply generating a stream of text. Whether that text happens to be valid HTML is entirely your responsibility as the developer. PHP can switch in and out of "PHP mode" as many times as you like within a single file — and this is exactly how PHP historically earned its reputation as a templating language, long before dedicated template engines became common.
echo vs print
Both output a string, but they behave a little differently:
| echo | ||
|---|---|---|
| Return value | None (not an expression) | Returns 1 (always) — it is an expression |
| Multiple comma-separated values | Yes: echo "a", "b"; |
No — single argument only |
| Performance | Marginally faster (negligible in practice) | Marginally slower (negligible) |
| Usage | Both are language constructs, not real functions — that's exactly why you don't need parentheses: echo "hi";, not echo("hi"); |
|
echo "Hello", " ", "World"; // valid, outputs: Hello World
print "Hello" . " " . "World"; // valid — must concatenate, one arg only
Practical guidance: use echo. It's the overwhelmingly dominant convention in modern PHP code, and you'll rarely find a real reason to prefer print. I'm teaching you both because you'll definitely run into print when reading other people's code — and now you know exactly why it behaves slightly differently. Its return value of 1 is occasionally, rarely, exploited in obscure expressions — not something you need to worry about doing yourself.
Edge Case Worth Knowing: Alternative Syntax
You can technically close and reopen PHP tags mid-statement-block for conditional HTML output — a very common pattern in old-school PHP templating:
<?php if ($isLoggedIn): ?>
<p>Welcome back!</p>
<?php else: ?>
<p>Please log in.</p>
<?php endif; ?>
This alternative syntax (if: / endif;) exists specifically to make PHP-in-HTML more readable than nesting curly braces { } inside HTML. You'll see this a lot in Phase 9 when we build real HTML output — for now, just recognize it as valid PHP, not a typo.
How This Connects Forward
This ties directly back to the request/response lifecycle from 1.1 — the "output buffer" concept above is literally what becomes the HTTP response body — and it sets up Phase 2 (Variables), since virtually every real PHP statement you write from here on manipulates variables between these tags.
Knowledge Check — Phase 1 (All Sections)
A. Concept Questions
- What's the difference between a compiler and an interpreter, in your own words?
- Why does PHP "not remember" anything between two separate page loads by default?
- What's the difference between PHP CLI mode and PHP web mode?
- What does
php -S localhost:8000actually start, and why is it not meant for production? - Why should a pure-PHP file (with no trailing HTML) omit the closing
?>tag? - What is
php.ini, and name two settings it controls. - In terms of return value, how does
printdiffer fromecho?
B. Code Prediction
What will this output? Reason through it line by line before running it.
<?php
echo "Start";
?>
<p>Middle content</p>
<?php
echo "End";
C. Debugging Challenge
Find and fix every syntax error in this snippet:
<?php
echo "Hello world"
echo "Second line";
# this is fine
/* unclosed comment
echo "Third line";
D. Practical Challenge
Write a .php file that:
- Outputs an
<h1>tag containing the text "My First PHP Page" usingecho. - Then, using PHP-in-HTML embedding (closing and reopening tags, like the
<h1>example above but for a<p>), outputs a paragraph containing the text "This paragraph was written using pure HTML, not echo." - Run it via
php -S localhost:8000and confirm both lines render correctly in the browser.
Work through A, B, C, and D. Post your answers and code below — I'll review before we move on to Phase 2: Variables and Data Types.
Comments (0)
Leave a Comment