Results 1 to 3 of 3

Thread: return

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default return

    why is it that when i do this, echo does not output the string?
    Code:
    $s = 'string';
    return $s;
    echo $s;
    Last edited by ggalan; 05-04-2011 at 12:11 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

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

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    return does two things: 1. it determines the output value for the function; 2. it stops execution of the function at the current point. If you have any complicated structures such as if statements, then it can be difficult to know exactly when the function will end. This can also be useful however:
    PHP Code:
    function myfunction($x) {
       if (
    $x==true) { return true; }
       return 
    false;

    That is an example of using return as a default value. It acts just like an "else" statement in this case, because if the if statement was true then it would have already exited the function.


    Note that in your example above it is not within a function, so I don't know what return will do in that situation.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. The Following User Says Thank You to djr33 For This Useful Post:

    ggalan (05-04-2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •