How do I convert function output to function return

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

How do I convert function output to function return

Post by Luke »

I don't really know how to word this... I have a function that just dumps information to the browser. I remember reading something on php.net about a function that can take output and put it in a variable. I can't find it ANYWHERE now. anybody know what it is??

Code: Select all

logerror("Could not post event: ".getdetails($_SESSION));
getdetails is a function that just dumps information about a variable onto the browser. How do I take that information and put it in a variable so I can save it into a log?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

output buffering.. ob_start() will get you where you want.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I remember there was some function that did this:

Code: Select all

$variable = whateverthefunctionwas(print("Text text text"));
Does anybody know what it is?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I just told you, and was kind enough to give you a link to it.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I know. I appreciate it very much. Thanks. But it's not what I'm looking for. The function I found was much simpler that that.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there is none, last I saw that does "exactly" what you wrote in your previous example. Besides, the code you posted would send the result (1) to the function in question... why not add some functionality to your getdetails() function that you can tell it to return the string or echo it, it shouldn't be that complicated..
Last edited by feyd on Wed Aug 17, 2005 6:21 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

All i can think of is: (Just add http://www.php.net/ before the name of the functions to look them up..)

ob_get_contents
print_r
eval
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

In your function code, getdetails(), take the information that is being echo'ed and assign that data to a variable. For example:

Code: Select all

function getdetails($some_data)
{
    $return_data = '';
    if (is_array($some_data)
    {
        foreach($some_data as $data_value)
        {
            $return_data .= $data_value . '<br />';
        }
    }

    return $return_data;
}
Is this what you are looking for? Using this you could easily write to the HTML the value of the information returned from what was passed to your function.

Hope I read your question correctly.
Post Reply