🚀 Intermediate PHP Mastery

Level up your PHP skills with forms, sessions, databases, and real-world applications. Get ready for interactive challenges!

PREMIUM Video lessons & source code available.

📊 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".

💡 Pro Tip: Use `global` or `$GLOBALS` to access global variables inside functions. Prefer dependency injection for cleaner code.

💻 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);
?>
Download Examples

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).

Only letters and spaces allowed
10-500 characters

💻 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)

1

Times you've visited (session)

Cookie Data

Not set

Your saved preference

Last Activity

Just now

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

1 | John | john@email.com
2 | Jane | jane@email.com
3 | Bob | bob@email.com
4 | Alice | alice@email.com
5 | Charlie | charlie@email.com

📋 products

1 | Laptop | 999.99
2 | Mouse | 29.99
3 | Keyboard | 79.99
4 | Monitor | 299.99
5 | Headphones | 149.99

📋 orders

1 | 1 | 2024-01-15
2 | 2 | 2024-01-16
3 | 3 | 2024-01-17
4 | 1 | 2024-01-18
5 | 4 | 2024-01-19
Query result will appear here...

📁 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!

What is the correct way to declare a static variable inside a function?

⚡ 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
💡 Starter Code: Check the example below for hints!
<?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; }
?>
Download Template

📥 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!

Upgrade to Premium Learn More