Advanced Search

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-03-2011 at 11:11 PM.

  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
    11,797
    Thanks
    227
    Thanked 657 Times in 645 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>| Deutsch | italiano | español | português | català | un peu de français | Ninasoma Kiswahili | 日本語の学生でした。| درست العربية

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

    ggalan (05-03-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
  •