我可以使用ob_start包含一个包含变量的外部php文件吗?

is this possible and if yes how would i go by this approach, i don't really get what is the purpose of ob_start since i have not used this function and i don't really know when to use this function.

i have already asked a similar question here but failed to get any answer, so i am hoping that with this more accurate question i could now get a better answer to this dilemma, i also understand that there other ways for me to do this via SVN but i would like to continue with the approach of including an external file.

thank you.

the OB (output buffering) system only affects OUTPUT. it captures anything that would normally be sent to the remote browser and stores it in a memory buffer. As far as your average piece of PHP code is concerned, nothing has changed, except output has been temporarily trapped.

OB is handy in some cases, e.g. for whatever reason you might be producing output, but can't have it be sent out yet, e.g.

echo 'this will break the next line';
header("Location: otherpage.php");

Adding in output buffering would allow the header redirect to work:

ob_start();
echo 'this would have broken the next line, but output has been trapped';
header("Location: otherpage.php");
echo ob_get_clean(); // output actually occurs here

As another poster mentioned, ob_start doesn't have any effect on input to your program.

If you want to include a PHP file full of variables to an individual script, I recommend using require_once (http://php.net/manual/en/function.require-once.php). This will evaluate the given PHP file in the current scope if and only if it hasn't been evaluated before. I say to use the require_once function because include will not tell you if the file failed to load and the _once aspect of the structure ensures that you won't reload the file if multiple PHP files are accessed (which can be a headache).

If you're looking to bring variables from a file into EVERY php program that runs on your server, consider the php.ini directive auto_prepend_file (http://us3.php.net/manual/en/ini.core.php#ini.auto-prepend-file). This directive will load a file (much like include or require) for every request before evaluating your scripts.

If this isn't what you're looking for, can you clarify what you're looking for?

EDIT: Example from comments

$myStringArray = file('http://somewhere.com/file.txt');  //get the file contents as an array of lines

$myEvaluationString = ''; //set up a string which we will eventually evaluate

foreach ($myStringArray as $line) {

  $myEvaluationString = "$line
"; //loop over each line and add it to our evaluation string

}

$myEvaluationString = rtrim($myEvaluationString); //clean off the trailing newline

eval($myEvaluationString); //evaluate the string