Pure PHP solution to an assignment