By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,813 Members | 2,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,813 IT Pros & Developers. It's quick & easy.

capturing print output of function to variable

P: n/a
Hi all,

I have a function, say:

function printTable($Variables)
{
?>
<table>
<tr><td>Your name is</td></tr>
<tr><td>
<?php
print $Name;
print "</td></tr></table>";
}

(Obviously not my exact function, but a simple one to demonstrate my
needs... The function I have inherited is much bigger with much mixed php
and html like the example)

It is currently used to print the same data table on many pages.
I have been asked to have the system send this data table by email, so
what I want to do is run the function, but send the output of the function
to a variable, in the same way that print_r($arr,1) will return the output
instead of printing it.

Is this possible?

Cheers,

Ben
Jun 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ben Holness wrote:
Hi all,

I have a function, say:

function printTable($Variables)
{
?>
<table>
<tr><td>Your name is</td></tr>
<tr><td>
<?php
print $Name;
print "</td></tr></table>";
}

(Obviously not my exact function, but a simple one to demonstrate my
needs... The function I have inherited is much bigger with much mixed php
and html like the example)

It is currently used to print the same data table on many pages.
I have been asked to have the system send this data table by email, so
what I want to do is run the function, but send the output of the function
to a variable, in the same way that print_r($arr,1) will return the output
instead of printing it.

Is this possible?

Cheers,

Ben

Try output buffering.

ob_start();

callMyNiftyFunction();

$contents = ob_get_contents();

ob_end_clean();
Jun 25 '06 #2

P: n/a
Joe Estock wrote:
Ben Holness wrote:
Hi all,

I have a function, say:

function printTable($Variables)
{
?>
<table>
<tr><td>Your name is</td></tr>
<tr><td>
<?php
print $Name;
print "</td></tr></table>";
}

(Obviously not my exact function, but a simple one to demonstrate my
needs... The function I have inherited is much bigger with much mixed php
and html like the example)

It is currently used to print the same data table on many pages.
I have been asked to have the system send this data table by email, so
what I want to do is run the function, but send the output of the
function
to a variable, in the same way that print_r($arr,1) will return the
output
instead of printing it.

Is this possible?

Cheers,

Ben

Try output buffering.

ob_start();

callMyNiftyFunction();

$contents = ob_get_contents();

ob_end_clean();

Jun 25 '06 #3

P: n/a
Ben Holness wrote:
Hi all,

I have a function, say:

function printTable($Variables)
{
?>
<table>
<tr><td>Your name is</td></tr>
<tr><td>
<?php
print $Name;
print "</td></tr></table>";
}

I have been asked to have the system send this data table by email, so
what I want to do is run the function, but send the output of the function
to a variable, in the same way that print_r($arr,1) will return the output
instead of printing it.


Like this?

function printTable($Variables, $return = false)
{
$tableHtml = "
<table>
<tr><td>Your name is</td></tr>
<tr><td>$Name</td></tr>
</td></tr></table>";

if($return){
return $tableHtml;
} else {
echo $tableHtml;
return;
}
}
Jun 25 '06 #4

P: n/a
> Try output buffering.

ob_start();

callMyNiftyFunction();

$contents = ob_get_contents();

ob_end_clean();


Perfect! Thanks,

Ben
Jun 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.