include into var - scope problem
| From: | Carlos Henrique | Date: | Thu, 19 Oct 2000 14:08:04 +0000 |
| Subject: | include into var - scope problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-21164@lists.php.net to get a copy of this message | ||
Wow, thats ob_ functions are great! I never used it before! That's my
option. Eval() works fine too but to parse the files I need to put ending
php tag in the start and starting php tag in the end. Yuck!
Now I need to do this include thing a lot of times in some pages of the
site. If I create a function to do that and set some variables (used in the
include file) before calling the function these will not be available
because the local scope of function. But I can't define global $var in
function cause I don't know how many and which variables are needed by the
include file. There's any way to do that, do a generic function, something
like sending all the local variables to the function?
function parse_fileread ($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
Regards,
Carlos
> But for this work the content in "file.php" needs to be something like:
> >
> > return "<table
> > border=1>...<b><?=$varone;?></b>...</table>";
> >
> > How can I do the same thing without placing all the file into a
> > return() statement?
> >
> > Regards,
> > Carlos
>
> Two options: you'd either need to suck the code into a string and run
> eval() on it, or use the output buffering functions to trap output to
> a variable for the duration of the inclusion: something like this:
>
> <?php
> error_reporting(E_ALL);
>
> ob_start();
> include('test.html');
> $included_file = ob_get_contents();
>
> /* Next line gets output buffer contents & clears buffer. */
> ob_end_clean();
>
> echo "Got this text from the included file:\n" .
> htmlspecialchars($included_file);
>
> ?>