The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Page include not able to read variables

I have a main script that I include classes/functions along with initializing variables. The problem Im having is Im using a function that reads an include and puts the result into the ob buffer/string so I can echo it out at a later time:

That's because functions in PHP have their own scope, i.e. they are there own little universes and can only see data explicitly handed to them (via input parameters) and global variables. If you need to make some variables available to your script that is included inside of a function, pass the variables to the function; you could, if you needed to, pass in an associative array to allow for arbitrary number of variables:

Code PHP:

$vars=array('variable'=>'This is a test','variable2'=>'This is also a test');$pagecontent= getPageContent('pages/mypage.php',$vars);function getPageContent($pagefile,$vars=NULL){if(!is_null($vars)){foreach($varsas$var=>$val){$$var=$val;}}//now include your page and do what you need to}

edit: err, maybe you can't do that now that I think about it, since you are including the file from within the function.
edit2: wait a minute, now I see what you are trying to do. Yes, global is what you need.

That's because functions in PHP have their own scope, i.e. they are there own little universes and can only see data explicitly handed to them (via input parameters) and global variables.

I get that, I guess what I am asking is how do I get the script I include inside the function access to all variables/functions from inside the main script as each include script does something different. Sounds like it cant be done in the current way.

Globals are easier, yes, but frowned upon by most PHP developers. It's bad practice in general. Better is to be explicitly handing values to your functions rather than merely global-izing your variables, because you're only serving to open yourself up to the most bizarre and arcane of bugs if a function changes a value it's not supposed to or you're not expecting it to. This is also why passing values is preferred over passing references.