From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Treating the final element differently with CachingIterator

Treating the final element differently with CachingIterator - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Treating the final element differently with CachingIterator

- In courses.xml, the subjects and software elements have an indeterminate number of children. Some have only one, like this example of software here. But in other courses, it might have two, three or even more types. When displaying multiple values, it would be useful to know if any more follow the current element so we can treat the last one differently by inserting the word "and" in front of it. That's easily done with the CachingIterator class. So let's just open this page, caching.php, which you can find in the Chapter 6 folder of the exercise files. It loads courses.xml as a simple XML and then a foreach loop goes through the courses displaying the title and description of each course. To display the software related to each course, we need to get the software element and then retrieve its child element. So let's add a new line inside the foreach loop, and we'll store the software elements as software. So software = course and then we access its software property and then we…

Contents