SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
     
    //filename: function/pagecontent.php
    function getPageContent($pagefile)
    {
     
       
    ob_start();
       include_once(
    $pagecontent);
       
    $buffer ob_get_contents();
       
    ob_end_clean();
       return 
    $buffer;
     
     

    The file Im including is local to the webserver. But when I include the function in my main script the $pagecontent page that it processes cannot see the variables from the main script:

    PHP Code:
    //main script
     
    $variable 'This is a test';
     
    require_once(
    'function/pagecontent.php');
     
    //If I try to echo out $variable in pages/mypage.php it would come back blank.
    $pagecontent getPageContent('pages/mypage.php'); 
    So using this function how would I get pages/mypage.php to be able to read the variables from the main script?

    Thank you
    Sillysoft

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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($vars as $var=>$val)
            {
                $$var = $val;
            }
        }
        //now include your page and do what you need to
    }
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The easier way is to get access to the globals using the global keyword:

    Code:
    function getPageContent($pagefile)
    {
       global $var1, $var2; // etc
    
       ob_start();
       include_once($pagecontent);
       $buffer = ob_get_contents();
       ob_end_clean();
       return $buffer;
     
    }
    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.
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  4. #4
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kromey View Post
    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.

    Silly

  5. #5
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    PHP questions? RTFM
    MySQL questions? RTFM


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •