This is a sample code from the book I am reading:
ob_start();
include("{$path}.ini");
$string = ob_get_contents();
ob_end_clean();
$pairs = parse_ini_string($string);
My question is, how does ob_get_contents() know what to get contents from? ({$path}.ini in this situation)?
ob_get_contentssimply gets the contents of the output buffer since you calledob_start(). Essentially, an output buffer in PHP catches anything that would have been output to the browser (excluding headers). It’s useful in cases where you may need to filter some output, or you’re using a PHP method (such asvar_dump) that writes output directly to screen, and you would instead like the return value of the method in a string.In this case, because you’re
include()ing the.inifile, it’s contents will be essentially output to screen, andob_get_contents()will get the content of the file.If you were to put
echo "I'm a little teapot short and stout";underneath theinclude, this would also be included in$stringafter the body of the.inifile.In your specific case, however, output buffering is an unnecessary overhead, simply use
file_get_contentson the.inifile. I’m not sure why a book would even have this code in it at all.