Re: Differences

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04 Oct 2012 at 01:48, David McGlone <david@xxxxxxxxxxxxx> 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.

It's a poor example, for one thing. As others have said, echo and return have nothing to do with each other.

> 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.

I assume that where you have "function filename" above, you really mean "function completeImageFilename".

Why do you think that the return you have coded above should give more than one filename when you've written "return $filename"? That's one filename. You then echo that out with your echo statement (because it's the return value of the function), and that's what you see. If you change the return to an echo, then instead of returning after once round your while loop, the while loop goes round and round dealing with each filename you have in $matches, and echoes out each one. When the while loop has completed, the function returns, but returns nothing at all. So your original echo then does nothing at all. And so you see the observed behaviour.

--
Cheers  --  Tim

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux