Created
May 14, 2015 22:29
-
-
Save christophherr/52b27222b75af34a6549 to your computer and use it in GitHub Desktop.
WordPress Developers Club Challenge - Object Buffering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. | |
//Hello World11. | |
//ob_flush sends the contents of the buffer. | |
//The first content is the echo "Hello World" and the second content is the string length (strlen) of "Hello World". | |
//Hello World echoes no matter what unless the buffer is "cleaned/destroyed" with ob_clean or ob_end_clean... | |
//2. | |
//Render out "Hello World" | |
//(I don't think I understood this correctly...) | |
<?php | |
ob_start(); | |
echo "Hello World"; | |
ob_flush(); | |
?> | |
// Played around a bit and liked how this used the echoed value as variable outside of the buffer. | |
<?php | |
ob_start(); | |
echo "Hello World"; | |
$output_buffer = ob_get_contents(); | |
ob_end_clean(); | |
echo ( $output_buffer ); | |
?> | |
//It was fun playing around with this. | |
// Thanky you for the brain teaser, Tonya! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Q1 - Perfect. Well done!
Q2: Yes that works. Here's other ways to do it too:
ob_end_flush() does the following:
If we needed to capture the contents to return, say in a filter for HTML (which I do all the time), then you can do:
Excellent job!