Introduction

Welcome to today's lesson! We'll be exploring how to manage student enrollments for various courses using PHP. Imagine you're running an online course platform and need to handle enrollments, checks, and listings of students in different courses. PHP's associative arrays are perfect for this kind of problem since they allow straightforward management of unique student enrollments per course.

By the end of this session, you'll be well-versed in using PHP associative arrays for such tasks. Let’s dive in!

Introducing Methods to Implement

Here are the methods we need to implement in our enrollment system:

  • function enroll($student, $course): Adds a student to a course. If the student is already enrolled, it does nothing.
  • function unenroll($student, $course): Removes a student from a course. Returns true if the student was enrolled and has now been removed, otherwise returns false. If, after unenrolling the student, the course becomes empty (no one is enrolled there), remove the course as well.
  • function isEnrolled($student, $course): Checks if a student is enrolled in a course. Returns true if the student is enrolled and false otherwise.
  • function listStudents($course): Returns an array of all students enrolled in a given course. If no students are enrolled, it returns an empty array.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal