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!
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. Returnstrue
if the student was enrolled and has now been removed, otherwise returnsfalse
. 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. Returnstrue
if the student is enrolled andfalse
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.
