Jump to content

Defined variable scope in WHILE loop, included file


BDabrowski

Recommended Posts

I have an array, $_SITE. It's defined early on and I have no problem using it anywhere except here.

 

In an included file, I run a mysql query and use while to grab the rows. If I try to print one of the elements of $_SITE, it doesn't show up.

 

I moved the code up out of the included file and it printed fine. Also, within the included file, if I put global $_SITE inside the WHILE loop, it works.

 

Shouldn't defining functions be the only limitation of scope?

index.php

 

includes settings.php

 

 

$_SITE['root_pages_dir'] = $_SITE['root_inst_dir'] . 'pages/';

 

includes second_file.php

 

  includes inc.last_file.php

 

$qs = "SELECT * FROM " . DBPFX . "pages";

$q = mysql_query($qs);

while ($r = mysql_fetch_assoc($q)) {

    
    $page_dir = $_SITE['root_pages_dir'] . $r['pages_dir'];
    
    echo $page_dir;
    
    if (!is_dir($page_dir)) {
    
        echo $page_dir;
    
        //mkdir($page_dir);
    
    }

}

 

When the code is in inc.last_file.php, it needs the

global $_SITE;/php] line. If it's in [i]second_file.php[/i], it works fine. 

Why don't you show how you are including the files.

 

We just went through this with some other member where he insisted that he did not need to show the actual relavant code. Had he done so, his problem would have been fixed in a few minutes rather than the day+ that it actually took.

Because it's a flow that includes several files and several more directories and I thought I was making it easier to follow. To include every file in it's entirety seemed unnecessarily complex. They're just

 include 'file.php';

.

 

I've thrown in a few characters of text just to prove to myself that they are including properly.

 

I'll see if I can get more, but I was putting it out to see if there was any general rules I was missing like using $_VARIABLE convention to name a variable or anything else obvious.

 

 

You were right. The manner in which I included them was the problem.

 

I was using a function to load all files in a directory if the directory is present. That function did not have the global $_SITE call. Thus any functions included from it did not have the $_SITE variable.

 

Sorry. Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.