That was a typo in the post and not the code. Sorry for the confusion.
When I use this syntax in my code I do get $title but it just prints one, and not foreach. If you know what I mean.
If you want to collect all titles and use them in your main program, just store them in an array (use push for that) and return the array after(!) the foreach loop.