PHP: Include(), how to only get $Var

Discussion in 'Web Design and Development' started by iTim314, Feb 18, 2008.

  1. iTim314 macrumors 6502

    Joined:
    Jun 5, 2005
    Location:
    U.S.
    #1
    Lets say I have about 4 php files within a directory. I want to include all those files... but I only want to get a certain variable from those pages to be displayed on one page.

    PHP:
        foreach(glob("ResourceFile-*.php") as $GlobalFile)
            {
                include_once(
    $GlobalFile);
                echo 
    $Variable;
            }
    Inside each file, "ResourceFile-1.php, ResourceFile-2.php, etc," is a variable set as....

    PHP:
    $Variable .= "Content 1";
    ...respectively.

    I know the include(), or include_once() isn't the way to go in the glob() script. I don't want all the variables on the ResourceFiles-*.php, just one specific one, so that It'll echo "Content 1, Content 2," etc.

    Sorry if my wording is a bit weird, I really can't think of a better way to put it. How would I go about doing this?
     
  2. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    Would it be a better idea to simply modify some shared global variable from each included script?
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    Not sure why you're trying to do it this way. Why not have some preferences file that has the values from the PHP files and just parse it out? Or you can make one PHP file that simply setups these needed variables and include that file. I do this by having my mail variables or database variables in one file, and just include it where/when I need it.
     
  4. alfaphlex macrumors newbie

    Joined:
    Feb 18, 2008
    #4
    It's pretty hard to give you the right answer without knowing more details, but in general, what you have is a scope problem. IMO, the simplest solution would be to implement that snippet of code as a function, so that the included file dont affect any variables outside the function scope. Something like:
    PHP:

    function getVarFromResource($resource$varname){
        foreach(
    glob($resource) as $GlobalFile)
           {
               include_once(
    $GlobalFile);
               echo $
    $varname;
           } 
    }
    that way you'd be able to use it with any filename you wanna glob(). as well as any variable you want to display from the globbed files, ie:
    PHP:
    // the file you wanna glob()
    $filename "ResourceFile-*.php";

    // the name of variable you wanna display
    $varname "Variable";

    getVarFromResource($filename,$varname);

    // now lets display any other variable in you globbed files
    $varname "Test";

    // This would display any $Test variables
    getVarFromResource($filename,$varname); 
    However, the problem with the above is that if any of the files that you include() outputs anything (i.e- echo), that content will be displayed automatically. To avoid that and display ONLY the variable you want, this would be more appropriate:
    PHP:
    function getVarFromResource($resource,$varname){
        foreach(
    glob($resource) as $GlobalFile)
           {
               
    // start output buffering (nothing after this gets displayed)
               
    ob_start();

               include_once(
    $GlobalFile);
               
               
    // retrieve the variable you want
               
    $bufferedvar = $$varname;

               
    // clean the buffer (anything that would've been displayed is cleared)
               
    ob_clean();

               echo 
    $bufferedvar;
           } 
    }
    If any of your other files have any echo statements, they'll be ignored. Any variable in the included files will not affect the global scope and you have a reusable function that you can apply to any future similar situations.

    Hope any of this helps.
     
  5. macgruder macrumors 6502

    Joined:
    Oct 29, 2007
    Location:
    UK
    #5
    PHP:
    $i=0;
     foreach(
    glob("ResourceFile-*.php") as $GlobalFile)
            {

                include_once(
    $GlobalFile);
                
    $foo[$i++] = $Variable;
              
            }

    Then grab what you need from the array.
     

Share This Page