scriptygoddess

08 Feb, 2003

Eval Function and Controlling the Output Buffer

Posted by: adam In: Lessons learned

Ok… this is my first post here, so I'm going to start out relatively small.

While I was working on the update of my flip gallery script today I needed a way to check a variable for a particular string, and if that string didn't appear I wanted to execute any php code that was embedded in the variable (which was the contents of Movable Type's Extended Entry field). Then I wanted to dump the output into a new variable. The eval() function worked perfectly for this:

ob_start();
eval("?>" . $entry_text . "<?php ");
$result_text = ob_get_contents();
ob_end_clean();

ob_start(), ob_get_contents(), and ob_end_clean() are functions that control output buffering. Basically, ob_start() causes php to begin caching output rather than sending it to the browser, ob_get_contents() returns all the cached results, and ob_end_clean() empties the output buffer and turns output buffering off again.

Feel free to check out the Flip Gallery code if you want to see how this works.

The output buffer catches all output, not only script output:

<? ob_start(); ?>
stuff, more stuff, even more stuff
<?
$normal_text = ob_get_contents();
ob_end_clean();
print "Output Buffer: " . $normal_text;
?>

3 Responses to "Eval Function and Controlling the Output Buffer"

1 | Jennifer

February 9th, 2003 at 1:24 pm

Avatar

I once read a book about how to be a good "manager" and one of the first rules was to hire people who were "better than you". Based on this post, I think I've done just that by adding you! LOL! I don't want to bug the hell out of you – but I'd love it if you could give me a better explanation of all that.

Question 1) "ob_start() causes php to begin caching output rather than sending it to the browser"
What type of output do you mean? The only thing I would think that would be sent to the browser would be anything specifically in an "echo" or "print" (or would your "stuff, more stuff, etc." also be "cached")

Question 2) "$normal_text = ob_get_contents();"
Is that just taking whatever would have been output to the screen inside the variable "$normal_text"? (ie. "stuff, more stuff, etc.")

Question 3) "eval("?>" . $entry_text . "<?php ");"
I don't really get the eval() function. From what I read in the PHP manual – it looks like you put PHP code inside eval()… so in your case, $entry_text has some php code in it (WITH the <? ?> which is why I'm guessing you have the ?> and <? around entry_text) – and by putting it inside eval() it will actually EXECUTE that code… ie…
$entry_text = "<? echo 'hello'; ?>";
if you just did:
echo $entry_text;
you'd see this:
<? echo 'hello'; ?>
but if by doing eval($entry_text);
it would render
hello
however – (here's my question finally) eval doesn't return anything – is that why you need the ob_start, ob_get_contents… is that what actually grabs the "result" of eval()?

2 | adam

February 9th, 2003 at 4:05 pm

Avatar

Question 1: It caches all output, even that which isn't generated by print and echo. I put together a little test that starts content buffering at the top of the page, fetches the source of my main page, and replaces < and %gt;, and puts the whole thing within a <pre> tag.

<?
ob_start();
>?

<? include("http://www.gessaman.com/index.php");>?

<?
$page = ob_get_contents();
ob_end_clean();

$page = str_replace(">" , "&gt;", $page);
$page = str_replace("<" , "&lt;", $page);
$page = ereg_replace("\n", "\r\n", $page);
header("Content-Type: text/html");
print "<pre>" . $page . "</pre>";
?>

Question 2: Exactly.

Question 3: See this explaination for the odd use of eval("?>" . $entry_text . "&lt?php "); Basically, because eval() processes any string as if it were code, you need to trick it into not attempting to execute everything that is outside of <? and ?> If it tries, it just spits error after error.

As far as needing to control the buffer to grab the output of eval(), you're absolutely right. Otherwise, it would simply output whatever print/echo statements you had in your code where-ever you invoked eval().

One side note on controlling the buffer: It appears that if there are errors in your php, the buffering is turned off and the error is dumped immediately, so remember to suppress errors @function() or code flawlessly. *grin*

3 | Jennifer

February 9th, 2003 at 9:38 pm

Avatar

Cool – I think I have a better understanding. I think its one of those things, that I probably won't completely get until I have a project that uses it.. but at least I understand it enough to know it exists so when I hit the need, I'll know where to start. Thanks!!

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements