Skip to content

Instantly share code, notes, and snippets.

@biozshock
Created November 30, 2011 21:19
Show Gist options
  • Save biozshock/1410884 to your computer and use it in GitHub Desktop.
Save biozshock/1410884 to your computer and use it in GitHub Desktop.
Php nested ob_start explanation
<?php
// without OB
echo 'before_ob' . PHP_EOL;
ob_start(); // first level begins
// this line will be in $second
echo 'second_ob' . PHP_EOL;
ob_start(); // second level begins
// these 2 lines will be in $first
echo 'third_ob' . PHP_EOL;
echo '------' . PHP_EOL;
$first = ob_get_clean() . 1;
// this line will be in second
// but w/o previous 2 lines
echo '------' . PHP_EOL;
$second = ob_get_clean() . 2;
// this will output $first consisting of 2 lines.
// and $second without them.
var_dump($first, $second);
/**
Output:
before_ob
string(17) "third_ob
------
1"
string(18) "second_ob
------
2"
testscript: http://biozshock.com/test_ob.php
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment