PHP solution to problem 18