Home Quizzes Leaderboard Competitions Learn Hire Us
About Contact
Log In Sign Up
Learn PHP Final Project --- Student Management System

Final Project --- Student Management System

⏱ 30 min read read
What We Are Building:

A Student Management System that can add and remove students, record
grades per subject, compute averages and letter grades, generate a
sorted report, save data to CSV, and handle errors gracefully.

Concepts Used in This Project:

Lesson 2 --- variables and types (string, float, int, array)

Lesson 7 --- functions with type hints and default arguments

Lesson 8 --- classes and encapsulation (Student class with private
properties)

Lesson 9 --- interfaces (Reportable) and abstract classes

Lesson 11 --- exception handling (invalid grade raises ValueError)

Lesson 12 --- file I/O (saving report to CSV with fopen/fputcsv)

Lesson 15 --- enum (Grade) for type-safe grade letters

Lesson 17 --- array_map, array_filter for processing student lists

After This Course --- What to Build Next:

1. Learn Laravel to build full-stack web applications with MVC.

2. Build a REST API with Laravel or Slim Framework.

3. Explore PHP connected to MySQL with PDO for real data persistence.

4. Learn PHPUnit for test-driven development.

5. Explore WordPress plugin development --- PHP powers most of the web.

-----------------------------------------------------------------------
Congratulations on completing all 20 PHP lessons! 🎉

-----------------------------------------------------------------------
Code Example
<?php

declare(strict_types=1);

// Enum for grade letters (PHP 8.1+)

enum GradeLetter: string {

case A = 'A'; case B = 'B'; case C = 'C'; case D = 'D'; case
F = 'F';

}

// Student class

class Student {

private array $grades = [];

public function \_\_construct(

private readonly string $id,

public string $name,

public int $age

) {}

public function addGrade(string $subject, float $grade): void {

if ($grade < 0 || $grade > 100) {

throw new InvalidArgumentException("Grade must be 0--100, got
$grade");

}

$this->grades[$subject] = $grade;

}

public function getAverage(): float {

return empty($this->grades) ? 0.0

: array_sum($this->grades) / count($this->grades);

}

public function getLetterGrade(): GradeLetter {

$avg = $this->getAverage();

return match(true) {

$avg >= 90 => GradeLetter::A,

$avg >= 80 => GradeLetter::B,

$avg >= 70 => GradeLetter::C,

$avg >= 60 => GradeLetter::D,

default => GradeLetter::F,

};

}

public function getId(): string { return $this->id; }

public function \_\_toString(): string {

return sprintf('[%s] %s (age %d) | Avg: %.1f (%s)',

$this->id, $this->name, $this->age,

$this->getAverage(), $this->getLetterGrade()->value

);

}

}

// School class

class School {

private array $students = [];

public function \_\_construct(private string $name) {}

public function enroll(Student $s): void {

foreach ($this->students as $existing) {

if ($existing->getId() === $s->getId()) {

throw new RuntimeException("ID already exists: {$s->getId()}");

}

}

$this->students[] = $s;

echo "Enrolled: {$s->name}\n";

}

public function findById(string $id): ?Student {

foreach ($this->students as $s) {

if ($s->getId() === $id) return $s;

}

return null;

}

public function printReport(): void {

$sorted = $this->students;

usort($sorted, fn($a, $b) => $b->getAverage() <=>
$a->getAverage());

echo "\n=== {$this->name} Report ===\n";

echo str_repeat('-', 55) . "\n";

foreach ($sorted as $s) echo " $s\n";

echo str_repeat('-', 55) . "\n";

$avg = array_sum(array_map(fn($s) => $s->getAverage(),
$this->students)) / count($this->students);

$passing = count(array_filter($this->students, fn($s) =>
$s->getAverage() >= 60));

echo sprintf("School Average: %.1f\n", $avg);

echo "Passing: $passing / " . count($this->students) . "\n";

}

public function saveToCsv(string $filename): void {

$handle = fopen($filename, 'w');

fputcsv($handle, ['ID', 'Name', 'Age', 'Average',
'Grade']);

foreach ($this->students as $s) {

fputcsv($handle, [$s->getId(), $s->name, $s->age,

number_format($s->getAverage(), 1),
$s->getLetterGrade()->value]);

}

fclose($handle);

echo "Saved to $filename\n";

}

}

// Run it

$school = new School('PHPAcademy');

$alice = new Student('S001', 'Alice', 20);

$bob = new Student('S002', 'Bob', 22);

$carol = new Student('S003', 'Carol', 21);

$dave = new Student('S004', 'Dave', 23);

$alice->addGrade('Math', 92); $alice->addGrade('English',
88); $alice->addGrade('Science', 95);

$bob->addGrade('Math', 65); $bob->addGrade('English', 72);
$bob->addGrade('Science', 60);

$carol->addGrade('Math', 78); $carol->addGrade('English',
85); $carol->addGrade('Science', 82);

$dave->addGrade('Math', 45); $dave->addGrade('English', 55);
$dave->addGrade('Science', 50);

foreach ([$alice, $bob, $carol, $dave] as $s) {
$school->enroll($s); }

$school->printReport();

$found = $school->findById('S002');

if ($found) echo "\nFound: $found\n";

try { $alice->addGrade('PE', 150); }

catch (InvalidArgumentException $e) { echo "\nError: " .
$e->getMessage() . "\n"; }

$school->saveToCsv('students.csv');

?>
← Design Patterns ✓ Back to Course →

Log in to track your progress and earn badges as you complete lessons.

Log In to Track Progress