📊 Intermediate Progress
Completed: 60% | Next Level: Advanced PHP
1. 📚 Functions and Variable Scope
🎯 Learning Objectives:
- ✓ Defining and calling functions
- ✓ Global, local, static scope
- ✓ Closures and use keyword
- ✓ Recursive functions
📖 Deep Dive:
Functions help you write DRY code. PHP supports variable scope (global, local) and static variables that persist between calls. Closures can inherit variables from the parent scope using "use".
💻 Advanced Function Example:
<?php
// Global scope
$counter = 0;
function incrementCounter() {
global $counter;
$counter++;
static $staticCount = 0;
$staticCount++;
return ['global' => $counter, 'static' => $staticCount];
}
$multiplier = 3;
$multiply = function($num) use ($multiplier) {
return $num * $multiplier;
};
function factorial($n) {
if ($n <= 1) return 1;
return $n * factorial($n - 1);
}
echo 'First call: ' . print_r(incrementCounter(), true);
echo 'Multiply 5 by 3: ' . $multiply(5);
?>
2. 📝 Form Handling & Validation
🎯 Learning Objectives:
- ✓ POST vs GET
- ✓ Input sanitization & validation
- ✓ CSRF basics
- ✓ File uploads
🎮 Interactive Form Playground
Submit this form to see simulated PHP handling (data stored in sessionStorage).
💻 Server-side Validation Code:
<?php
// Secure form handling with validation
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = [];
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
if (!preg_match('/^[A-Za-z ]{2,50}$/', $name)) {
$errors[] = 'Invalid name format';
}
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email format';
}
if (empty($errors)) { /* process */ } else { /* show errors */ }
}
?>
3. 🍪 Sessions & Cookies Management
Session Data (sessionStorage)
Times you've visited (session)
Cookie Data
Your saved preference
Last Activity
Update on page load
💻 Session & Cookie Code:
<?php
session_start();
$_SESSION['user_id'] = 123;
setcookie('theme', 'dark', time() + 86400 * 30, '/');
if (isset($_COOKIE['theme'])) { echo $_COOKIE['theme']; }
?>
🎮 Database Query Challenge
Write SQL queries to interact with the simulated database!
📋 users
📋 products
📋 orders
📁 File Upload & Handling
Drop files here or click to upload
Supports: .txt, .php, .html, .css, .js (max 2MB)
💻 PHP File Upload Code:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
$targetDir = 'uploads/';
$fileName = basename($_FILES['file']['name']);
$targetFile = $targetDir . $fileName;
$fileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
if ($_FILES['file']['size'] > 2000000) { echo 'File too large!'; exit; }
$allowedTypes = ['txt', 'php', 'html', 'css', 'js'];
if (!in_array($fileType, $allowedTypes)) { echo 'File type not allowed!'; exit; }
if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) { echo 'Uploaded!'; }
}
?>
📝 Intermediate Level Quiz
Test your intermediate PHP knowledge!
⚡ Code Challenge: Build a Login System
Using sessions and form handling, create a simple login system!
Requirements:
- Create a login form with username and password
- Validate inputs (username: 3-20 chars, password: min 6 chars)
- Store login status in session
- Create a logout button
- Show different content for logged-in users
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
if ($username === 'admin' && $password === 'password123') {
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $username;
header('Location: dashboard.php');
exit;
} else { $error = 'Invalid credentials'; }
}
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
echo 'Welcome, ' . $_SESSION['username'] . '!';
}
if (isset($_GET['logout'])) { session_destroy(); header('Location: ' . $_SERVER['PHP_SELF']); exit; }
?>
📥 Intermediate Resources
Download practice files, cheat sheets, and project templates
💎 Unlock Advanced Features
Get access to premium content including video tutorials, source code, and certification!