π PHP Fundamentals: Your First Steps into Web Magic
π The Story Begins: Meet PHP, Your Kitchen Chef
Imagine youβre building a restaurant. Your HTML is the menu that customers see. Your CSS is the beautiful tablecloth and decorations. But who actually cooks the food? Thatβs PHP!
PHP is like a chef in the kitchen. Customers never see the chef, but the chef prepares everything and sends out the finished dishes. The waiter (your web browser) only delivers what the chef made.
π What is PHP?
PHP = Hypertext Preprocessor
PHP is a server-side programming language. This means it runs on the server (the restaurant kitchen), not on your computer (the dining table).
Simple Example:
<?php
echo "Hello, World!";
?>
This code tells PHP to send the words βHello, World!β to the browser.
Real Life Uses:
- π Online stores like WooCommerce
- π Blogs like WordPress
- π± Social media features
- π§ Contact forms on websites
Why PHP?
| Feature | What It Means |
|---|---|
| Free | Costs nothing to use |
| Easy | Simple to learn |
| Powerful | Runs 78% of websites |
| Friendly | Works with HTML easily |
βοΈ PHP Execution Model: How the Magic Happens
Think of ordering pizza online:
graph TD A[π§ You click Order] --> B[π‘ Request goes to server] B --> C[π¨βπ³ PHP processes your order] C --> D[π PHP creates HTML response] D --> E[π Browser shows Thank You page]
The 5-Step Dance:
- You click a button on a website
- Request flies to the server
- PHP runs on the server
- PHP creates HTML (like a chef plating food)
- Browser receives and shows the HTML
Key Point:
π‘ Your browser never sees PHP code. It only sees the finished HTML that PHP created!
Example:
You write this PHP:
<?php
$name = "Alex";
echo "Hello, $name!";
?>
Browser receives this HTML:
Hello, Alex!
The PHP code stays secret on the server. Only the result travels to you!
π·οΈ PHP Version Differences
PHP has grown over time, like a child becoming an adult. Here are the major versions:
Version Timeline:
| Version | Year | Big Changes |
|---|---|---|
| PHP 5 | 2004 | Object-oriented programming |
| PHP 7 | 2015 | 2x faster! Better errors |
| PHP 8 | 2020 | Named arguments, JIT compiler |
| PHP 8.3 | 2023 | Newest features |
What Changed From 7 to 8?
PHP 7 (Old way):
<?php
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet("Sam");
?>
PHP 8 (New way - Named Arguments):
<?php
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet(name: "Sam");
?>
Which Version Should You Use?
π― Use PHP 8.1 or higher for new projects. Itβs faster and safer!
π§ PHP Setup and Configuration
Before cooking, you need a kitchen! Hereβs how to set up PHP:
Option 1: All-in-One Package (Easiest! π)
graph TD A[Download XAMPP or MAMP] --> B[Install it] B --> C[Start Apache & MySQL] C --> D[β Ready to code!]
| Tool | Best For |
|---|---|
| XAMPP | Windows, Linux, Mac |
| MAMP | Mac (but works on Windows) |
| Laragon | Windows (super fast) |
Option 2: Check if PHP is Installed
Open your terminal and type:
php -v
You should see something like:
PHP 8.2.12 (cli)
The Config File: php.ini
This file controls PHPβs behavior. Important settings:
; Show errors (for learning)
display_errors = On
; Time limit for scripts
max_execution_time = 30
; Memory limit
memory_limit = 128M
π‘ Tip: In production (real websites), set
display_errors = Offfor security!
π·οΈ PHP Tags and Embedding in HTML
PHP lives inside HTML! You tell the server where PHP starts and ends using special tags.
The Standard Tag (Use This! β )
<?php
// Your PHP code here
?>
Mixing PHP with HTML
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome!</h1>
<?php
$hour = date("H");
if ($hour < 12) {
echo "<p>Good Morning! βοΈ</p>";
} else {
echo "<p>Good Afternoon! π€οΈ</p>";
}
?>
<p>Regular HTML here</p>
</body>
</html>
Short Echo Tag (For Quick Output)
<!-- Instead of this: -->
<?php echo $name; ?>
<!-- Use this: -->
<?= $name ?>
Important Rules:
| Do This β | Not This β |
|---|---|
<?php ... ?> |
<? ... ?> (old, unreliable) |
| Close tags in HTML files | Leave open in pure PHP files |
π PHP Syntax Basics
PHP syntax is like English sentences with rules.
Rule 1: End Lines with Semicolon
<?php
echo "Hello"; // β
Has semicolon
echo "World"; // β
Has semicolon
?>
Rule 2: Variables Start with $
<?php
$name = "Luna";
$age = 10;
$is_happy = true;
?>
Rule 3: PHP is Case-Sensitive (Sometimes!)
<?php
$Name = "Alice";
$name = "Bob";
// These are DIFFERENT variables!
echo $Name; // Alice
echo $name; // Bob
// But functions are NOT case-sensitive:
ECHO "Hi"; // Works
echo "Hi"; // Also works
?>
Rule 4: Strings Use Quotes
<?php
$single = 'Hello'; // Single quotes
$double = "Hello $name"; // Double quotes (variables work!)
?>
Quick Reference:
graph TD A[PHP Statement] --> B[Keyword/Variable] B --> C[Operator] C --> D[Value] D --> E[Semicolon ;]
π¬ Comments: Notes for Humans
Comments are notes that PHP ignores. They help YOU remember what your code does!
Single-Line Comments
<?php
// This is a comment
echo "Hello"; // Comment at end of line
# This also works (less common)
?>
Multi-Line Comments
<?php
/*
This comment
spans multiple
lines!
*/
echo "World";
?>
DocBlock Comments (Professional Style)
<?php
/**
* Calculate the area of a rectangle
* @param int $width The width
* @param int $height The height
* @return int The area
*/
function area($width, $height) {
return $width * $height;
}
?>
When to Use Comments:
| Use Comments For | Example |
|---|---|
| Explaining WHY | // Skip weekends for business logic |
| TODO reminders | // TODO: Add validation |
| Tricky code | // Using bit shift for speed |
π‘ Pro Tip: Good code is like a good jokeβif you have to explain it, itβs not that good! Write clear code first, then add comments only where needed.
π’ Echo and Print: Talking to the World
These commands send text to the browser. Theyβre how PHP βspeaksβ!
Echo (Most Common β)
<?php
echo "Hello, World!";
echo "Multiple", " ", "strings"; // Can take many values
?>
Print (Slightly Different)
<?php
print "Hello, World!";
// print "One", "Two"; // β Error! Only ONE value
?>
Echo vs Print:
| Feature | Echo | |
|---|---|---|
| Speed | Tiny bit faster | Tiny bit slower |
| Values | Multiple | Only one |
| Return | Nothing | Returns 1 |
Outputting Variables
<?php
$name = "Star";
$age = 5;
// Method 1: Concatenation (dots)
echo "Name: " . $name . ", Age: " . $age;
// Method 2: Double quotes (cleaner!)
echo "Name: $name, Age: $age";
// Method 3: Curly braces (complex variables)
echo "Name: {$name}, Age: {$age}";
?>
Outputting HTML
<?php
echo "<h1>Welcome!</h1>";
echo "<p style='color:blue;'>Blue text</p>";
echo "<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>";
?>
The Short Echo Tag
<!-- In HTML files, use this shortcut: -->
<p>Hello, <?= $name ?></p>
<!-- Instead of: -->
<p>Hello, <?php echo $name; ?></p>
π Congratulations!
Youβve learned the foundations of PHP! Hereβs what you can now do:
graph TD A[β Understand what PHP is] --> B[β Know how PHP runs] B --> C[β Set up PHP locally] C --> D[β Mix PHP with HTML] D --> E[β Write basic syntax] E --> F[β Add comments] F --> G[β Output with echo/print]
Your First Mini-Project:
Try this code in a file called greeting.php:
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
// Get current hour
$hour = date("H");
$name = "Future Developer";
// Choose greeting based on time
if ($hour < 12) {
$greeting = "Good Morning";
$emoji = "βοΈ";
} elseif ($hour < 18) {
$greeting = "Good Afternoon";
$emoji = "π€οΈ";
} else {
$greeting = "Good Evening";
$emoji = "π";
}
?>
<h1><?= $emoji ?> <?= $greeting ?>, <?= $name ?>!</h1>
<p>The time is <?= date("h:i A") ?></p>
<!-- PHP version info -->
<footer>
<small>Powered by PHP <?= phpversion() ?></small>
</footer>
</body>
</html>
π Next Step: Practice! The best way to learn PHP is by writing PHP. Start small, make mistakes, and keep building!