On Thursday, October 04, 2012 03:01:12 AM Timmy Sjöstedt wrote:
> Hi David,
>
> A "return" statement will immediately halt execution of the current
> function and return to where it was called.
>
> In your case, the foreach loop will execute once and find a return
> statement, and thus halting execution of the function and returning only
> the first filename.
>
> echo() is simply another function call (except it's a language construct
> and not a function) and will not halt execution as "return" does.
That's what I was looking for.
>
> What you want to do is something like:
>
> $filenames = array();
> foreach ($matches as $filename) {
> $filenames[] = $filename;
> }
> return $filenames; // this is now an array containing all the filenames
I see where I wasn't thinking correctly. I thought that $filename had already
contained all the results.
>
> But this is rather unneccesary, as $matches already is an array and
> contains everything you need. Thus all you have to do is:
>
> return $matches;
I had thought this also, and tried this but didn't get any results. probably
because I did something wrong somewhere else and didn't realize it.
>
> Which in turn can be shortened to:
>
> function filename($prefix)
> {
> return glob('images/property_pics/'. $prefix .'*');
> }
I'll mess around with this and see what I can learn..
David M.
>
> Happy Thursday!
> Timmy
>
> On 2012-10-04 02:48, David McGlone wrote:
> > Hi everyone, I have been playing around with some code the list helped me
> > with a while back and I'm not grasping the concept between return and
> > echo and the PHP manual doesn't answer this, unless I'm missing
> > something. There is an example at the very bottom of PHP's return manual,
> > but it's confusing.
> >
> > So now I'm left wondering why return will only give me the first result in
> > an array, but echo will give me all the results of the array. Using
> > stuart's example he had sent me a while back I've messed around with it
> > and modified it to better understand it:
> >
> > function filename($prefix)
> > {
> >
> > $matches = glob('images/property_pics/'.$prefix.'*');
> > foreach($matches as $filename){
> > return $filename;
> >
> > }
> >
> > }
> >
> > echo completeImageFilename($row['MLS_No']);
> >
> > With the above code I only get the first image of each picture name, but
> > when I change return to echo, it groups and displays all the pics that
> > have the same picture name.
> >
> >
> > --
> > David M.
--
David M.