Re: Differences

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

 



On Wed, Oct 3, 2012 at 9:01 PM, James <james@xxxxxxxxxxxxxxx> wrote:
> All of the images are displaying because you're simply instructing the function to print out each file found with your call to glob(). The glob() function returns an indexed array containing files found in the path you specified, or an empty array if no files were found or false if glob() failed. When I say "print" I'm referring to you using the echo language construct, however, print is also another language construct.
>
> Therefore using echo in your function allows the foreach loop to continue iterating through the array of files returned by glob(). Replacing that echo with the return, the function ones one iteration in the foreach loop and stops, returning that value. In your case, the function is returning index 0 of the array returned by glob().
>
> Make more sense?
>
> David McGlone <david@xxxxxxxxxxxxx> wrote:
>
>>On Wednesday, October 03, 2012 08:55:29 PM admin 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.
>>>
>>> The first loop and return is all you will get.
>>> Put the information into an array and return the array once the array
>>is
>>> built.
>>
>>I think I understand what your saying, but what I don't understand is
>>that
>>when I leave the current code intact and replace return $filename with
>>echo
>>$filename in the function, all the images display. Is this intended
>>behavior
>>between return and echo or is it just bad code on my part?
>>
>> --
>>David M.
>
> --
> Sent from my Android phone with K-9 Mail. Please excuse my brevity.

echo and return are not analogous items at all in PHP. They don't do
the same thing because their purpose is to do two entirely different
things.

For purposes of this discussion, the important bit about return is here:

"If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call." [1]

The concept comes from making a call to a function and then returning
from it, where you will use the value that is returned.

echo, on the other hand, is about *displaying* values:

"echo — Output one or more strings" [2]

Let me try a bogus analogy:

A friend gives you a dollar to buy a soda from the machine. You take
the dollar, go over to the soda machine and get the soda.

In the case of return, you hand your friend the soda.

In the case of echo, you drink the soda.



Perhaps an example that is out of the context you are looking at might help:


Example 1:
==========

function testreturn () {

  for ($i = 0 ; $i < 10 ; $i++ ) { // this will begin a loop where the
variable $i will start at value 0, and increase in value each time
through the loop by 1, stopping after $i is equal to 9.

    return $1; // this will "short circuit" the loop and the enclosing
function, or the php script if called from the global context. In
other words, it *ends* the loop *and* function right there, sending
the value of $i (0 in this case) back to the calling function or
context.

  }
}


Example 2:
=========

function testecho () {

  // Again, our for statement:
  for ($i = 0 ; $i < 10 ; $i++) {

    echo $i; // this will emit the current value of $i to standard
output. when it is done, the loop continues

  }

}

Here, there is no return from the function, nothing is passed back to
the calling function or context.


Let's take a look at your code again:

function completeImageFilename($prefix)
{
  $matches = glob('images/property_pics/'.$prefix.'*');
  foreach($matches as $filename){
    return $filename;
  }
}

echo completeImageFilename($row['MLS_No']);

When your loop contains nothing but the return $filename, it doesn't
matter how many entries are in your array, you are telling the
function to return with the first one in the array, no matter what.

If you modify it thusly:

function completeImageFilename($prefix)
{
  $matches = glob('images/property_pics/'.$prefix.'*');
  foreach($matches as $filename){
    echo $filename; // here you are telling it to send the value of
$filename to standard output
  }
   // there is no return value from the function
}

echo completeImageFilename($row['MLS_No']); // when you get here,
there is nothing to echo (or it attempts to echo NULL, which amounts
to the same thing.

(as a side note, the code you supplied would not work, as the name you
gave the function was "filename" yet the function you were trying to
call was "completeImageFileName".)

So, return is not echo, echo is not return.



[1] http://us3.php.net/manual/en/function.return.php
[2] http://us3.php.net/manual/en/function.echo.php

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