PHP Intro
PHP Intro: Server-Side Web Scripting with PHP 8
PHP (Hypertext Preprocessor) is an open-source, server-side scripting language built for web development. When a browser requests a PHP page, the web server runs the PHP script first and sends only plain HTML or JSON back to the user.
How PHP Runs on the Server
Because PHP executes on your backend server, visitors never see your source code, database passwords, or internal business rules.
flowchart LR
A["Browser Request"] --> B["Web Server (Nginx / Apache)"]
B --> C["PHP 8 Runtime"]
C --> D["MySQL Database"]
D --> C
C --> E["HTML or JSON Response"]
E --> A
Modern PHP 8 powers small business sites, custom REST APIs, and large platforms like WordPress and Laravel. It runs on Linux, macOS, and Windows servers without changing your application code.
Your First Dynamic PHP Page
You can mix PHP logic with HTML templates by wrapping server instructions inside <?php ... ?> tags. Always escape dynamic strings with htmlspecialchars() so user input cannot inject unsafe scripts into the page.
<?php
declare(strict_types=1);
$serverTime = date('Y-m-d H:i:s');
$userRole = 'Editor';
$activeModules = ['User Accounts', 'Invoice Generator', 'Email Queue'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Server Status</title>
</head>
<body>
<h2>PHP Server Information</h2>
<p>Server Time: <strong><?= htmlspecialchars($serverTime) ?></strong></p>
<p>Current Role: <span><?= htmlspecialchars($userRole) ?></span></p>
<h3>Enabled Modules</h3>
<ul>
<?php foreach ($activeModules as $module): ?>
<li><?= htmlspecialchars($module) ?></li>
<?php endforeach; ?>
</ul>
</body>
</html>
Common Mistakes to Avoid
- Opening
.phpfiles directly from your file explorer: Browsers cannot run PHP locally viafile://. Always serve files through a web server orphp -S localhost:8000. - Echoing raw user input: Printing
$_GETor database strings withouthtmlspecialchars()creates Cross-Site Scripting (XSS) vulnerabilities. - Mixing SQL queries inside HTML templates: Fetch and process data in dedicated PHP functions before rendering your view markup.
Frequently Asked Questions
Can browsers execute PHP code directly?
No. Browsers only understand HTML, CSS, and JavaScript. The web server executes PHP first and returns the rendered output to the browser.
Is PHP still used for new projects?
Yes. PHP 8 offers typed properties, JIT compilation, and mature frameworks like Laravel and Symfony that power millions of modern production APIs and websites.
What file extension does a PHP script require?
Save PHP scripts with the .php extension so the web server knows to pass the file to the PHP interpreter instead of serving it as plain text.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.