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
Hello PHP
Your first PHP page
Create a PHP page that displays "Hello, World!" and demonstrates basic PHP syntax including variables, echo statements, and embedding PHP in HTML.
Detailed Instructions
- Create a PHP file: Save as "hello.php"
- Add PHP opening tag: Start with <?php
- Create variables: Store your name and a greeting message
- Use echo: Display the greeting with your name
- Mix HTML and PHP: Add HTML structure around PHP code
- 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
Variables and Data Types
Working with PHP variables
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()orgettype()to show variable types - Perform basic operations (addition, concatenation)
- Display results in a formatted HTML table
Submit Your Work
Form Processing Exercises
Contact Form Handler
Process form submissions
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
Sessions & Cookies Exercises
Simple Login System
Using sessions for authentication
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
Database Operations Exercises
User Registration System
Database CRUD operations
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
<?phptry {
$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();
}
?>