BDabrowski Posted February 9, 2010 Share Posted February 9, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/191501-defined-variable-scope-in-while-loop-included-file/ Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 can we see the code? Quote Link to comment https://forums.phpfreaks.com/topic/191501-defined-variable-scope-in-while-loop-included-file/#findComment-1009483 Share on other sites More sharing options...
BDabrowski Posted February 9, 2010 Author Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191501-defined-variable-scope-in-while-loop-included-file/#findComment-1009498 Share on other sites More sharing options...
PFMaBiSmAd Posted February 9, 2010 Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191501-defined-variable-scope-in-while-loop-included-file/#findComment-1009510 Share on other sites More sharing options...
BDabrowski Posted February 9, 2010 Author Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191501-defined-variable-scope-in-while-loop-included-file/#findComment-1009528 Share on other sites More sharing options...
BDabrowski Posted February 9, 2010 Author Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191501-defined-variable-scope-in-while-loop-included-file/#findComment-1009534 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.