Exercises Overview

Welcome to the Part 4 exercises! These hands-on activities will help you master PHP for server-side web development. Complete each exercise to build your skills in creating dynamic web applications.

Your Progress

Complete exercises to track your progress

PHP Basics Exercises

4.1

Hello PHP

Your first PHP page

Beginner

Create a PHP page that displays "Hello, World!" and demonstrates basic PHP syntax including variables, echo statements, and embedding PHP in HTML.

Detailed Instructions

  1. Create a PHP file: Save as "hello.php"
  2. Add PHP opening tag: Start with <?php
  3. Create variables: Store your name and a greeting message
  4. Use echo: Display the greeting with your name
  5. Mix HTML and PHP: Add HTML structure around PHP code
  6. Test the page: Run it on a PHP server (XAMPP, WAMP, or live server)

PHP Template

<!DOCTYPE html>
<html>
<head>
    <title>Hello PHP</title>
</head>
<body>
    <?php
    $name = "Your Name";
    echo "<h1>Hello, " . $name . "!</h1>";
    ?>
</body>
</html>

Submit Your Work

Please compress your PHP file and any related files into a ZIP archive. Maximum file size: 5MB.
4.2

Variables and Data Types

Working with PHP variables

Beginner

Create a PHP script that demonstrates different data types: strings, integers, floats, booleans, and arrays. Display each variable's value and type.

Requirements

  • Create variables of each data type
  • Use var_dump() or gettype() to show variable types
  • Perform basic operations (addition, concatenation)
  • Display results in a formatted HTML table

Submit Your Work

Maximum file size: 5MB.

Form Processing Exercises

4.3

Contact Form Handler

Process form submissions

Intermediate

Create a contact form with fields for name, email, and message. Build a PHP script that processes the form, validates the input, and displays a success message or error messages.

Requirements

  • Create an HTML form with name, email, and message fields
  • Validate that all fields are filled
  • Validate email format using filter_var()
  • Sanitize input using htmlspecialchars()
  • Display success message or validation errors
  • Prevent XSS attacks by sanitizing output

Form Processing Template

<?php
$errors = [];

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');

    // Validation
    if (empty($name)) {
        $errors[] = "Name is required";
    }
?>

Submit Your Work

Include both the HTML form and PHP processing file. Maximum file size: 5MB.

Sessions & Cookies Exercises

4.4

Simple Login System

Using sessions for authentication

Intermediate

Create a simple login system with a login form and a protected page. Use sessions to track logged-in users and redirect unauthorized access.

Requirements

  • Create a login form (username and password)
  • Validate credentials (hardcoded for simplicity)
  • Start a session and store user data
  • Create a protected page that checks for session
  • Add a logout functionality
  • Redirect unauthorized users to login page

Submit Your Work

Include login.php, protected.php, and logout.php files. Maximum file size: 5MB.

Database Operations Exercises

4.5

User Registration System

Database CRUD operations

Advanced

Create a user registration system that stores user data in a MySQL database. Include form validation, password hashing, and display a list of registered users.

Requirements

  • Create a MySQL database and users table
  • Build a registration form with validation
  • Use PDO for database connection
  • Use prepared statements for security
  • Hash passwords using password_hash()
  • Create a page to display all registered users
  • Handle errors gracefully

Database Connection Template

<?php
try {
    $pdo = new PDO(
        "mysql:host=localhost;dbname=your_database",
        "username",
        "password"
    );
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
?>

Submit Your Work

Include all PHP files and a SQL file with table structure. Maximum file size: 5MB.