Lesson 1 โข Beginner
Introduction to PHP ๐
Learn what PHP is, how it runs on the server, and why it powers over 77% of the web โ from WordPress to Facebook.
What You'll Learn in This Lesson
- โข What PHP is and why it's the most popular server-side language
- โข How server-side code differs from client-side JavaScript
- โข The request-response cycle: browser โ server โ PHP โ HTML โ browser
- โข PHP syntax basics: echo, variables, comments, and string concatenation
- โข Setting up your PHP development environment
php -S localhost:8000 to start a local server.1๏ธโฃ What is PHP?
PHP (Hypertext Preprocessor) is a server-side scripting language designed for web development. Unlike JavaScript which runs in the browser, PHP runs on the web server and generates HTML that gets sent to the user.
| Feature | Details |
|---|---|
| Created | 1995 by Rasmus Lerdorf |
| Latest Version | PHP 8.3 (2024) |
| Market Share | 77% of websites with known server-side language |
| Famous Users | WordPress, Wikipedia, Facebook, Slack |
| File Extension | .php |
2๏ธโฃ PHP Syntax Basics
PHP code is written inside <?php ?> tags and can be embedded directly in HTML files:
<!DOCTYPE html>
<html>
<body>
<h1>Welcome</h1>
<?php
$name = "Alice";
echo "<p>Hello, $name!</p>";
?>
</body>
</html>Key syntax rules:
- Every statement ends with a semicolon
; - Variables always start with a dollar sign
$ - Strings are concatenated with the dot operator
.not+ echooutputs text to the page
Try It: Your First PHP Script
See PHP basics โ variables, echo, comments, and string concatenation (simulated in JavaScript)
// PHP Introduction โ Simulated in JavaScript
// In real PHP, code runs on the server inside <?php ?> tags
console.log("=== Your First PHP Script ===");
console.log();
// In PHP, you use 'echo' to output text
// echo "Hello, World!";
console.log("Hello, World!");
console.log();
// PHP can mix with HTML on the server
console.log("=== How PHP Embeds in HTML ===");
console.log();
console.log("<!DOCTYPE html>");
console.log("<html>");
console.log("<body>");
console.log(" <h1>My First PHP Page</h
...3๏ธโฃ How PHP Works
PHP follows a request โ process โ response cycle:
Browser โ HTTP Request โ Web Server โ PHP Interpreter
โ
Browser โ HTML Response โ Web Server โ Generated HTMLTry It: How PHP Works (Server-Side)
Understand the request-response cycle and see PHP vs JavaScript compared
// How PHP Works โ Server-Side Execution
console.log("=== How PHP Works: Step by Step ===");
console.log();
// Simulate the request-response cycle
console.log("1๏ธโฃ User types: www.example.com/index.php");
console.log(" โ Browser sends HTTP request to server");
console.log();
console.log("2๏ธโฃ Web server (Apache/Nginx) receives the request");
console.log(" โ Finds index.php file on disk");
console.log(" โ Passes it to the PHP interpreter");
console.log();
console.log("3๏ธโฃ PHP interpreter
...4๏ธโฃ Setting Up PHP
Three ways to start coding PHP:
php -S localhost:8000 โ no Apache needed!docker run -p 8080:80 php:8.3-apacheโ ๏ธ Common Mistakes
;$name, not just name. dot operatorerror_reporting(E_ALL) during development to catch mistakes early.๐ Quick Reference โ PHP Basics
| Syntax | Example | Description |
|---|---|---|
| echo | echo "Hello"; | Output text |
| $variable | $name = "Alice"; | Declare variable |
| . (dot) | "Hi " . $name | Concatenate strings |
| // | // comment | Single-line comment |
| <?php ?> | <?php echo "Hi"; ?> | PHP opening/closing tags |
๐ Lesson Complete!
You now understand what PHP is and how it works on the server! Next, dive into PHP variables and data types.
Sign up for free to track which lessons you've completed and get learning reminders.