PHP is a server-side scripting language that runs on the web server, not the browser.
Variables start with $, are loosely typed, and use dot (.) for concatenation.
PHP files can mix HTML and PHP using tags.
Performance insight: PHP 8.2+ with JIT can execute scripts 2-3x faster than PHP 7.
Production insight: The most common production issue is a missing semicolon or unclosed tag causing a blank white page — always check error logs first, not the browser.
Plain-English First
Imagine a restaurant. The HTML menu is what the customer sees — static, printed, unchanging. PHP is the kitchen — it takes orders, checks what's in stock, personalises the meal, and sends back exactly what that specific customer asked for. Every time a user visits a PHP-powered webpage, the kitchen fires up, cooks a fresh response, and delivers it. The customer never sees the kitchen — they just get their meal. That's PHP: invisible, powerful, and working hard behind the scenes.
There's a reason PHP is still running the backend of WordPress, Facebook's early codebase, Wikipedia, and millions of e-commerce stores. When the web needed a way to make pages dynamic — to show your name after you log in, to pull products from a database, to process a payment — PHP was the tool that made it possible, and it's been doing it reliably since 1994. Learning PHP isn't just learning a language; it's learning how the majority of the web actually thinks.
Before PHP existed, every webpage was just a static HTML file — like a printed flyer that said the same thing to everyone who read it. If you wanted to show a personalised dashboard, you had to manually write a different HTML file for every single user. That's obviously insane. PHP solved this by letting developers write logic inside their web pages — 'if this user is logged in, show THIS; if not, show THAT.' One template, infinite personalised outcomes. That's the core problem PHP was built to solve.
By the end of this article you'll understand exactly what PHP is and where it runs, you'll have written and executed your first real PHP script, you'll understand variables, echo, and basic data types, and you'll know the most common beginner mistakes so you can skip the hours of frustrated Googling that everyone else goes through.
What PHP Actually Is — and Where It Lives
PHP stands for 'PHP: Hypertext Preprocessor' — yes, the acronym contains itself, which is a programmer joke called a recursive acronym. Don't worry about that. What matters is what PHP does.
PHP is a server-side scripting language. 'Server-side' means the PHP code runs on the web server — a powerful computer somewhere in the world — not inside your visitor's browser. The browser never sees your PHP code. It only ever receives the final HTML output that PHP produces. This is completely different from JavaScript, which runs inside the browser itself.
Think of it like this: when you order a burger at a drive-through, you never see the kitchen. You just get the finished burger through the window. PHP is the kitchen. Your browser is the drive-through window. The HTML page is the burger.
PHP files have the extension .php. Inside them, you can write regular HTML, and whenever you need the server to do something dynamic — fetch data, run logic, personalise content — you drop into PHP code using special tags. The server processes those tags, replaces them with real output, and sends clean HTML to the browser. The browser is none the wiser.
first_php_page.phpPHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
// Everything between <?php and ?> is processed by the PHP server engine.// The result replaces these tags before being sent to the browser.// php_uname() returns info about the server OS — proof this runs on the server, not the browser.
$serverName = php_uname('n');
// date() formats the current server date/time — this changes every time the page loads.
$todayDate = date('l, F j, Y');
// phpversion() returns which version of PHP the server is running.
$phpVersion = phpversion();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MyFirstPHPPage</title>
</head>
<body>
<h1>Hello from the Server!</h1>
<!-- Weswitch back into PHP to output dynamic values using echo -->
<p>Today is: <?php echo $todayDate; ?></p>
<p>This page was served by: <?php echo $serverName; ?></p>
<p>RunningPHP version: <?php echo $phpVersion; ?></p>
</body>
</html>
Output
Hello from the Server!
Today is: Tuesday, June 10, 2025
This page was served by: MyWebServer
Running PHP version: 8.2.12
Why the browser never sees PHP:
Open DevTools in your browser (F12 → Elements) on any PHP-powered site. You'll only ever see HTML. PHP code is completely consumed by the server during processing. This is also why PHP is a secure choice for handling passwords and database queries — that logic never reaches the end user's machine.
Production Insight
The server-side nature of PHP means any output before a session_start() or header() call will cause a 'headers already sent' warning.
In production, this often happens because an included file has a stray space before <?php.
Rule: Never put any character (including whitespace) before the opening <?php tag in files that set headers or cookies.
Key Takeaway
PHP runs on the server, outputs HTML to the browser.
The browser never sees your PHP code.
This separation is fundamental — it's what makes PHP secure for server-side operations.
Variables, Data Types, and echo — The Building Blocks You'll Use Every Day
In PHP, a variable is a named container that holds a value. Think of it like a labelled box — you put something in the box, give the box a name, and you can refer to that name anywhere in your code to get the value back out.
Every PHP variable starts with a dollar sign $. That's non-negotiable — no dollar sign, no variable. After the dollar sign comes the name you choose. Good variable names are descriptive: $customerName is infinitely better than $cn or $x.
PHP is 'loosely typed', which means you don't have to tell PHP what kind of data you're storing. You just store it, and PHP figures out the type automatically. This is a blessing for beginners and occasionally a curse for experienced developers — we'll cover the gotcha that comes with this in the mistakes section.
The most important PHP function you'll use daily is echo. It outputs content — text, numbers, HTML — directly into the page. You'll use it constantly. There's also print, which does almost the same thing, but echo is faster, more common, and can output multiple values at once. Always use echo.
variables_and_types.phpPHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
// --- STRINGS: text wrapped in quotes ---
$customerFirstName = "Alice"; // Double quotes allow variable expansion inside them
$welcomeMessage = 'Welcome back, '; // Single quotes are treated as literal text — no expansion// --- INTEGERS: whole numbers, no decimal point ---
$itemsInCart = 3;
$customerAge = 28;
// --- FLOATS: numbers with decimal places ---
$itemPrice = 19.99;
$taxRate = 0.08; // 8% tax// --- BOOLEANS: true or false, nothing else ---
$isLoggedIn = true;
$hasProAccount = false;
// --- NULL: deliberately empty, no value assigned ---
$pendingOrder = null;
// gettype() tells you what PHP thinks the variable's type is — great for debugging
echo gettype($customerFirstName) . "\n"; // Outputs: string
echo gettype($itemsInCart) . "\n"; // Outputs: integer
echo gettype($itemPrice) . "\n"; // Outputs: double (PHP calls floats 'double')
echo gettype($isLoggedIn) . "\n"; // Outputs: boolean
echo gettype($pendingOrder) . "\n"; // Outputs: NULL// --- CONCATENATION: joining strings together using the dot (.) operator ---// This is PHP's way of gluing strings together — NOT the + sign like in JavaScript
$fullGreeting = $welcomeMessage . $customerFirstName . "!";
echo $fullGreeting . "\n"; // Outputs: Welcome back, Alice!// --- ARITHMETIC: PHP handles maths naturally ---
$orderTotal = $itemsInCart * $itemPrice; // 3 x 19.99 = 59.97
$taxAmount = $orderTotal * $taxRate; // 59.97 x 0.08 = 4.7976
$finalTotal = $orderTotal + $taxAmount; // 59.97 + 4.7976 = 64.7676// number_format() rounds a float to 2 decimal places — essential for any money calculation
$formattedTotal = number_format($finalTotal, 2); // "64.77"echo"Cart total for " . $customerFirstName . ": $" . $formattedTotal . "\n";
// Outputs: Cart total for Alice: $64.77// --- BOOLEAN output quirk: var_dump() is better than echo for booleans ---// echo on a boolean gives you '1' for true and '' (nothing) for false — confusing!
echo $isLoggedIn; // Outputs: 1 (not 'true' — a classic beginner confusion)echo"\n";
var_dump($isLoggedIn); // Outputs: bool(true) — much clearer for debugging
?>
Output
string
integer
double
boolean
NULL
Welcome back, Alice!
Cart total for Alice: $64.77
1
bool(true)
Watch Out: PHP calls floats 'double'
When you run gettype() on a decimal number like 19.99, PHP returns the string 'double' — not 'float'. Both words refer to the same thing (a floating-point number), but this surprises beginners who write code checking if gettype($price) === 'float' and wonder why it never matches. Always check for 'double' when using gettype() on decimal numbers, or better yet, use is_float($price) which returns true correctly.
Production Insight
Loosely-typed variables can cause subtle bugs in production when comparing values of different types.
For example, '123' == 123 is true but '123' === 123 is false. Always use === for comparisons to avoid type coercion issues.
This is the number one source of logic bugs in PHP applications, especially in payment calculations.
Key Takeaway
Every PHP variable starts with $. Use dot (.) for string concatenation.
Use === for strict comparisons to avoid type coercion bugs.
When debugging, var_dump() shows the actual type and value — far better than echo.
PHP Tags, Comments, and How a PHP File is Actually Structured
Understanding the structure of a PHP file is the thing that separates beginners who 'write PHP' from beginners who actually understand what they're doing. Let's get this locked in clearly.
PHP code must live inside opening and closing tags: <?php to start, ?> to end. Everything outside those tags is treated as raw HTML and sent to the browser exactly as written. Everything inside is processed by PHP. You can have multiple PHP blocks in a single file, switching in and out of PHP as many times as you need.
One important rule: if a file contains only PHP and no HTML at all (which is very common for backend logic files), you should use the opening <?php tag but deliberately omit the closing ?> tag. This prevents a nasty bug caused by accidental whitespace after the closing tag that can corrupt HTTP headers.
Comments in PHP come in three flavours: // for a single-line comment, # also for single-line (less common), and / ... / for multi-line comments. Comments are stripped out by PHP and never sent to the browser — they're purely for developers reading the code.
php_structure_explained.phpPHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/*
* Multi-line comment block.
* Use this at the top of files or above complex functions
* to explain the PURPOSE of the code, not just what it does.
*
* File: php_structure_explained.php
* Purpose: DemonstratePHP file structure and comment styles
*/
// Single-line comment: use for quick notes on a specific line
$siteName = "TheCodeForge"; // You can also put comments at the END of a code line
$articleYear = 2025;
# This also works as a single-line comment, but // is far more common in modern PHP
$authorName = "Alex";
?>
<!-- We are now OUTSIDEPHP — this is regular HTML the server passes straight through -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $siteName; ?> - LearnPHP</title>
<!--
This is an HTML comment — completely different from a PHP comment.
HTML comments ARE sent to the browser and visible in ViewSource.
PHP comments are NOT sent — they're consumed by the server.
Never put passwords or secrets in HTML comments!
-->
</head>
<body>
<?php
// We've jumped BACK into PHP to do some logic
$currentYear = date('Y'); // Gets the current 4-digit year from the server// A simple conditional — we'll cover these fully in the next articleif ($currentYear === $articleYear) {
$yearMessage = "This article is brand new!";
} else {
$yearMessage = "Written in " . $articleYear . ", still accurate today.";
}
?>
<!-- Back to HTML, but we echoPHP variables inline using the short echo tag -->
<h1>Welcome to <?php echo $siteName; ?></h1>
<p>Written by <?php echo $authorName; ?> — <?php echo $yearMessage; ?></p>
<p>Server year check: <?php echo $currentYear; ?></p>
</body>
</html>
<?php
/*
* IMPORTANT: If this were a pure PHPfile (no HTML), we would NOTinclude
* a closing ?> tag at the end. This prevents 'headers already sent' errors
* caused by invisible whitespace or newlines after the closing tag.
*
* Rule of thumb:
* MixedPHP + HTML file → include closing ?>
* PurePHP logic file → OMIT the closing ?>
*/
?>
Output
<!-- Rendered HTML sent to the browser (PHP code is completely gone) -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TheCodeForge - Learn PHP</title>
</head>
<body>
<h1>Welcome to TheCodeForge</h1>
<p>Written by Alex — This article is brand new!</p>
<p>Server year check: 2025</p>
</body>
</html>
Pro Tip: Use = as a shorthand for
Inside HTML, typing <?php echo $variable; ?> gets repetitive fast. PHP provides a shorthand: <?= $variable ?> — it's exactly equivalent and widely used in templates. It's been fully supported since PHP 5.4 and is perfectly safe in modern PHP. You'll see it everywhere in real codebases, so get comfortable reading it now.
Production Insight
The single most common production error from PHP file structure is 'headers already sent' caused by a space before <?php.
Always check for invisible characters (BOM, whitespace) in include files.
Rule: In pure PHP files, omit the closing ?> tag entirely — this eliminates an entire class of bugs.
Key Takeaway
PHP tags <?php ... ?> mark server-processed code.
For files with only PHP, omit the closing ?> to avoid header corruption.
The shorthand <?= $var ?> equals <?php echo $var; ?> and saves time.
Setting Up PHP Locally — Running Your First Real Script in Under 5 Minutes
You need a PHP environment to run PHP code. PHP is a server-side language, which means you can't just open a .php file in your browser like you can with HTML — the browser doesn't know how to process it. You need a PHP interpreter installed on your machine (or a server). Here are your two practical options.
Option 1 — PHP's built-in development server (Recommended for beginners). If you install PHP directly, you get a built-in web server perfect for local development. No Apache, no Nginx, no configuration nightmares. One command in your terminal and you're live.
Option 2 — XAMPP or Laragon (All-in-one stack). These tools install PHP, a web server (Apache), and a database (MySQL) in one click. Great if you want to build database-connected apps quickly. Laragon is the modern, faster alternative to XAMPP on Windows.
For this article, we'll use PHP's built-in server since it has zero setup complexity. Once PHP is installed (php.net/downloads), you literally just run one command.
hello_world_complete.phpPHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/*
* This is the complete, runnable first PHP script.
*
* HOWTORUNIT:
* 1. Save this file as hello_world_complete.php
* 2. Open your terminal in the same folder
* 3. Run: php -S localhost:8000
* 4. Open your browser and go to: http://localhost:8000/hello_world_complete.php
* 5. You should see the output below.
*
* Alternatively, run from terminal WITHOUT a browser:
* php hello_world_complete.php
*/
// Grab the current hour of the day from the server (0 = midnight, 23 = 11pm)
$currentHour = (int) date('G'); // date('G') returns 0-23, (int) converts string to integer// Decide what greeting to show based on the time of dayif ($currentHour >= 5 && $currentHour < 12) {
$timeGreeting = "Good morning";
$emoji = "☀️";
} elseif ($currentHour >= 12 && $currentHour < 17) {
$timeGreeting = "Good afternoon";
$emoji = "⛅";
} elseif ($currentHour >= 17 && $currentHour < 21) {
$timeGreeting = "Good evening";
$emoji = "🌆";
} else {
$timeGreeting = "Working late, are we";
$emoji = "🌙";
}
// The visitor's name would normally come from a login system or URL parameter// For now, we hardcode it — we'll replace this with real user data in later articles
$visitorName = "Future PHP Developer";
// PHP_EOL is a constant that outputs the correct line ending for the current OS// On Windows it's \r\n, on Mac/Linux it's \n — using PHP_EOL makes your code portableecho $timeGreeting . ", " . $visitorName . "! " . $emoji . PHP_EOL;
echo"------------------------------------------" . PHP_EOL;
echo"The server time is: " . date('H:i:s') . PHP_EOL;
echo"PHP version running: " . phpversion() . PHP_EOL;
echo"------------------------------------------" . PHP_EOL;
// strlen() counts the number of characters in a string — useful all the time
$nameLength = strlen($visitorName);
echo"Your name has " . $nameLength . " characters in it." . PHP_EOL;
// strtoupper() transforms a string to ALL CAPSecho"Shouting your name: " . strtoupper($visitorName) . PHP_EOL;
// str_word_count() counts how many words are in a string
$wordCount = str_word_count($visitorName);
echo"Your name contains " . $wordCount . " words." . PHP_EOL;
Output
Good afternoon, Future PHP Developer! ⛅
------------------------------------------
The server time is: 14:23:47
PHP version running: 8.2.12
------------------------------------------
Your name has 22 characters in it.
Shouting your name: FUTURE PHP DEVELOPER
Your name contains 3 words.
Interview Gold: Why can't you just open a .php file in a browser?
Browsers speak HTML, CSS, and JavaScript. They have no PHP engine built in. When you open a .php file directly in a browser, it either shows you the raw PHP source code or a blank page — because the browser has no idea what <?php means. PHP must run through a server (even a local development server) that processes the PHP tags and returns pure HTML. This is the fundamental difference between server-side and client-side languages.
Production Insight
Using PHP's built-in server is perfect for local development but never use it in production — it's single-threaded and blocks on every request.
For production, use PHP-FPM with Nginx or Apache's mod_php.
Rule: Built-in server is for dev only. Production requires a proper web server stack.
Key Takeaway
Run php -S localhost:8000 to start a local dev server.
Access files via http://localhost:8000/file.php, not file:///.
Built-in server is single-threaded — never use it in production.
Debugging PHP: How to Fix the Three Most Common Errors
Every PHP beginner hits the same three walls: the dreaded blank white page, the 'headers already sent' warning, and the 'undefined variable' notice. Let's break down each one so you recognise it immediately and know exactly what to do.
1. The blank white page. This is almost always a syntax error — a missing semicolon, an unclosed brace, a missing quote. PHP's default behaviour is to die silently and output nothing. The fix: enable error reporting. Add error_reporting(E_ALL); and ini_set('display_errors', 1); at the top of your script, or run php -l filename.php from the terminal to check syntax without executing the script.
2. 'Headers already sent' warning. This happens when your script tries to set a header (like a redirect with header('Location: ...')) after some output has already been sent to the browser. The output could be a single space before <?php, an echo statement, or even an invisible UTF-8 BOM character. The fix: ensure nothing — absolutely nothing — appears before the opening <?php tag. Also, if you're including files, check they don't have trailing whitespace after a closing ?> tag.
3. 'Undefined variable' notice. PHP flags this as a notice (not a fatal error), but it can lead to unexpected behaviour if the variable was supposed to come from user input. Always initialise variables before use. For form data, use $_GET['name'] ?? '' with the null coalescing operator to provide a default value.
Once you master these three fixes, you'll spend far less time staring at broken pages and more time building things.
debugging_example.phpPHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
// Enable maximum error reporting for debuggingini_set('display_errors', 1);
error_reporting(E_ALL);
// Simulate a missing semicolon error (uncomment to test)// $name = 'Alice' // Missing semicolon — will cause parse error and blank page// Simulate 'Headers already sent' (uncomment to test)// echo 'Some output'; // This sends output before header()// header('Location: /home'); // This will trigger a warning// Simulate undefined variable notice
$userInput = $_GET['name'] ?? 'Guest'; // Using null coalescing operatorecho"Hello, $userInput";
// Proper way to check if a variable existsif (isset($_POST['submit'])) {
// Only run if submit button was pressedecho"Form submitted!";
}
// Use var_dump() to inspect variables during debugging
$debugArray = ['key1' => 'value1', 'key2' => 42];
var_dump($debugArray);
?>
Output
Hello, Guest
array(2) {
["key1"]=>
string(6) "value1"
["key2"]=>
int(42)
}
The Three Walls of PHP Debugging
Blank page gatekeeper: guards syntax errors. Pass by enabling display_errors or running php -l.
Headers already sent gatekeeper: guards output order. Pass by ensuring zero whitespace before <?php and omitting closing ?> in pure PHP files.
Undefined variable gatekeeper: guards uninitialised data. Pass by using isset() or ?? operators.
Production Insight
In production, you should NEVER enable display_errors — it exposes file paths and database structure to attackers.
Always log errors to a file without revealing them to users.
Rule: display_errors = Off in production, but log_errors = On.
Key Takeaway
Enable error reporting during development: ini_set('display_errors', 1);
Use php -l to check syntax before runtime.
Never display errors in production — always log them.
● Production incidentPOST-MORTEMseverity: high
Blank White Page After Deployment — Missing Semicolon Crashed a Live Store
Symptom
The site displayed a completely blank white page for all users. No HTML output, no error message. The server returned a 200 OK status, but the body was empty.
Assumption
The team assumed the web server was down or the database was unreachable. They restarted Apache and MySQL multiple times with no effect. They checked network connectivity but missed the PHP error log.
Root cause
A developer had edited a config file and accidentally removed the semicolon at the end of a PHP statement. This caused a parse error, which triggered PHP's default behaviour of silently dying — outputting nothing to the browser and logging the error only if display_errors was enabled (which it wasn't on production).
Fix
Enable PHP error logging in production by setting error_reporting = E_ALL and log_errors = On in php.ini, and check the error log at /var/log/php_errors.log. Once the missing semicolon was found, restoring it fixed the site immediately.
Key lesson
Always enable error logging in production — never rely on display_errors, which shows errors to users and is a security risk.
Use a local development environment with display_errors enabled to catch syntax errors before deployment.
Implement a simple health check script that runs 'php -l' on all files before deployment to detect syntax errors.
Production debug guideHow to diagnose the three most common PHP issues on a local dev environment3 entries
Symptom · 01
Browser shows a completely blank white page — no HTML output at all.
→
Fix
This is almost always a PHP parse error (missing semicolon, unclosed brace, or mismatched quotes). Enable display_errors in your php.ini or add 'ini_set('display_errors', 1);' at the top of the script. If you can't edit the file, check the PHP error log (usually /var/log/php_errors.log). The exact error message will tell you the file and line number.
Symptom · 02
You see raw PHP code displayed in the browser — <?php echo 'hello'; ?> is visible as text.
→
Fix
Your server is not processing PHP. The file is being served as plain text. Check that the file has a .php extension, that PHP is installed and running (php -v in terminal), and that your web server (Apache/Nginx) is configured to pass .php files to the PHP processor. Often this happens when you open a PHP file directly via file:/// in your browser instead of http://localhost:8000/file.php.
Symptom · 03
You see 'undefined variable' or 'undefined index' warnings on the page.
→
Fix
PHP flags these as notices, not fatal errors. They indicate you're trying to use a variable or array key that hasn't been set. Check if the variable is spelled correctly and initialised before use. For arrays, use isset() to check before accessing: if (isset($_GET['name'])) { … }.
★ Quick Debug Cheat Sheet for PHP BeginnersThe three errors that stop every new PHP developer in their tracks — and the exact commands to fix them.
Blank white page — no error displayed−
Immediate action
Run php -l yourfile.php from the terminal to check for syntax errors. This will output 'Parse error: syntax error, unexpected ...' with line number.
Commands
php -l index.php
tail -f /var/log/php_errors.log
Fix now
Open the file at the indicated line and look for a missing semicolon, brace, or quote. Add the missing character. Then run php -l again until it says 'No syntax errors detected'.
Raw PHP code displayed in browser instead of executing+
Immediate action
Stop opening the file via file:///. Start PHP's built-in server in the project folder.
Commands
php -S localhost:8000
Open browser at http://localhost:8000/yourfile.php
Fix now
This works immediately. If your code still doesn't execute, verify the file extension is .php and there are no HTML tags outside <?php ?> blocks that might confuse the parser.
Warning: Cannot modify header information - headers already sent+
Immediate action
Check for whitespace or output before <?php tags. The most common cause is a space or newline before the opening <?php in a file that sets cookies or redirects. Remove any characters before <?php.
Commands
grep -n '<?php' yourfile.php | head -1
od -c yourfile.php | head
Fix now
Delete any whitespace before the first <?php tag. Also check included files — they may have trailing whitespace after ?> tags. Best practice: omit closing ?> tag in pure PHP files.
Feature / Aspect
PHP (Server-Side)
JavaScript (Client-Side)
Where it runs
On the web server before the page is sent
Inside the user's browser after the page loads
User can see the code?
No — only the HTML output is sent
Yes — fully visible in browser DevTools
Accesses databases?
Yes — directly and securely
Not directly — needs a server API in between
Reads/writes files on server?
Yes — full filesystem access
No — sandboxed in the browser
Changes page without reload?
No — requires a new HTTP request
Yes — can update the DOM live
File extension
.php
.js
Needs a server to run?
Yes — always
No — runs in any modern browser
Best used for
Login systems, databases, APIs, email
Animations, form validation, dynamic UI
Can they work together?
Yes — and they do on almost every modern site
Yes — PHP generates HTML, JS enhances it
Key takeaways
1
PHP runs on the server, not the browser
the browser only ever receives the final HTML output. Your PHP code is never exposed to the end user.
2
Every PHP variable starts with a dollar sign ($). Forgetting it is the single most common beginner syntax error
and the error message isn't always obvious about why.
3
Use the dot (.) operator to join strings in PHP, not the plus (+) sign. Using + on strings gives you arithmetic results, not concatenation.
4
For files that contain only PHP (no HTML), omit the closing ?> tag to prevent 'headers already sent' errors caused by invisible trailing whitespace.
5
Enable error reporting during development
ini_set('display_errors', 1); error_reporting(E_ALL);. In production, log errors but never display them to users.
6
Use PHP's built-in server (php -S localhost:8000) for local development, but never in production
it's single-threaded.
Common mistakes to avoid
3 patterns
×
Forgetting the dollar sign on a variable
Symptom
Writing 'customerName = "Alice"' instead of '$customerName = "Alice"' — PHP throws a parse error: 'unexpected token' or 'undefined constant'. The code either fails completely or produces unexpected output.
Fix
Every single variable in PHP starts with $. No exceptions. If your code isn't working and you're staring at it confused, scan every variable name and make sure the $ is there.
×
Using + to concatenate strings instead of the dot operator
Symptom
Writing '$fullName = $firstName + $lastName' produces 0 or unexpected numeric output because + is the arithmetic addition operator in PHP, not string joining. This trips up anyone coming from JavaScript or Python.
Fix
Use dot (.) for string concatenation: '$fullName = $firstName . " " . $lastName'. Remember: JavaScript uses +, but PHP uses . for strings.
×
Opening a .php file directly in the browser instead of running it through a server
Symptom
You either see raw PHP code printed on screen, or a completely blank page, or just the HTML parts without any dynamic output.
Fix
Always access PHP files through a server URL (like http://localhost:8000/file.php), never by double-clicking the file and opening it as file:///path/to/file.php. PHP must pass through a PHP interpreter — the browser has none.
INTERVIEW PREP · PRACTICE MODE
Interview Questions on This Topic
Q01JUNIOR
What does 'server-side' mean in the context of PHP, and why does it matt...
Q02SENIOR
What is the difference between single quotes and double quotes in PHP st...
Q03SENIOR
If PHP is loosely typed, why might that cause bugs, and what PHP functio...
Q04JUNIOR
What is the short echo tag and why is it useful? Explain its syntax and ...
Q01 of 04JUNIOR
What does 'server-side' mean in the context of PHP, and why does it matter for security?
ANSWER
Server-side means PHP code runs on the web server, not in the browser. The browser only receives the final HTML output. This matters for security because sensitive logic (database credentials, authentication checks, payment processing) never leaves the server. A user cannot view or modify PHP code through browser DevTools, unlike JavaScript which is fully exposed. This is why PHP is preferred for backend operations where security is critical.
Q02 of 04SENIOR
What is the difference between single quotes and double quotes in PHP strings — and when would you choose one over the other?
ANSWER
Double quotes interpret variable names and escape sequences (like \n for newline), while single quotes treat everything literally. Example: $name = 'Alice'; echo "Hello $name"; // Outputs: Hello Alice. echo 'Hello $name'; // Outputs: Hello $name. Use double quotes when you need variable interpolation or escape sequences. Use single quotes for literal strings — they're slightly faster because PHP doesn't parse them for variables.
Q03 of 04SENIOR
If PHP is loosely typed, why might that cause bugs, and what PHP function can you use to check a variable's type at runtime?
ANSWER
Loose typing can cause unexpected comparison results. For example, '123' == 123 returns true because PHP converts the string to a number before comparing. But '123' === 123 returns false because it checks both value and type. This can lead to logic bugs in conditionals, especially when dealing with user input or API responses. To check a variable's type at runtime, use gettype() which returns a string like 'string', 'integer', 'double', 'boolean', etc. For boolean checks, use is_string(), is_int(), is_float() etc. var_dump() also shows both type and value.
Q04 of 04JUNIOR
What is the short echo tag and why is it useful? Explain its syntax and any compatibility considerations.
ANSWER
The short echo tag <?= $variable ?> is a shorthand for <?php echo $variable; ?>. It's been fully supported since PHP 5.4 and is enabled by default in all modern PHP installations. It's useful in templates to reduce boilerplate when outputting variables. For example: <h1><?= $pageTitle ?></h1>. There's no performance difference compared to full <?php echo, but it makes code cleaner and faster to write. However, avoid using the even shorter <? tag (without 'php') which was deprecated and removed — only <?php and <?= are reliable.
01
What does 'server-side' mean in the context of PHP, and why does it matter for security?
JUNIOR
02
What is the difference between single quotes and double quotes in PHP strings — and when would you choose one over the other?
SENIOR
03
If PHP is loosely typed, why might that cause bugs, and what PHP function can you use to check a variable's type at runtime?
SENIOR
04
What is the short echo tag and why is it useful? Explain its syntax and any compatibility considerations.
JUNIOR
FAQ · 6 QUESTIONS
Frequently Asked Questions
01
Is PHP still worth learning in 2025?
Absolutely. PHP powers roughly 77% of all websites with a known server-side language, including WordPress (which runs 43% of the entire internet), Wikipedia, and countless e-commerce platforms. The ecosystem is mature, the job market is steady, and PHP 8.x has modern features that make it genuinely enjoyable to write. It's not trendy, but it pays the bills reliably.
Was this helpful?
02
What's the difference between PHP and HTML?
HTML is a markup language that describes the structure of a page — it's static and looks the same for every visitor. PHP is a programming language that runs on the server and generates HTML dynamically. Think of HTML as a printed form and PHP as the person filling it out differently for each visitor based on who they are and what they requested.
Was this helpful?
03
Do I need to know HTML before learning PHP?
A basic understanding of HTML helps a lot because PHP is most commonly used to generate HTML pages. You don't need to be an HTML expert — knowing tags like <p>, <h1>, <a>, and <form> is enough to get started. As you build real PHP projects, you'll naturally learn more HTML alongside it.
Was this helpful?
04
Why do I see raw PHP code when I open a .php file in my browser?
Your browser doesn't have a PHP interpreter. When you open a file directly via file:///, the browser just reads the file's contents and displays them as text. To run PHP, you need a server (like the built-in server via php -S localhost:8000) that processes the PHP tags before sending the result to the browser. Always access PHP files via http://localhost:... URLs.
Was this helpful?
05
What is the difference between echo and print in PHP?
Both output text to the page. The key differences: echo can output multiple strings separated by commas (echo 'a', 'b'), print returns a value (1) so it can be used in expressions, and echo is slightly faster because it doesn't return a value. In practice, echo is used universally and print is rarely seen outside legacy code.
Was this helpful?
06
How do I know which PHP version I'm running?
Run php -v from your terminal to see the installed version. You can also use phpversion() within a PHP script. The current stable versions are PHP 8.2 and 8.3. Always target at least PHP 8.0 for modern features and security support.