techiefreak05 Posted August 14, 2010 Share Posted August 14, 2010 Hello all, I've run in to a very strange problem that I've never seen before. in my "config.php" file I have something like this... <?php // the URL of the script. NO trailing slash. $xconfig['url'] = $r['cfg_site_url']; // the site name $xconfig['title'] = $r['cfg_site_title']; // the YouTube user name to populate the site with. $xconfig['youtube_user'] = $r['cfg_site_youtube']; // the video to show on the homepage $xconfig['homepage_video'] = $r['cfg_homepage_video']; ?> and in my index.php page, I have <?php include "config.php"; ?> but, also in index.php, I have this <?php echo $xconfig['title'] ; ?> but it won't echo! None of the values in $xconfig[] output anything. I've even done put this in index.php <?php var_dump($xconfig); exit; ?> and that shows the array with the contents I expected. So, why does the array have the correct contents, but will not echo? Thanks a ton! Quote Link to comment https://forums.phpfreaks.com/topic/210711-array-has-contents-but-wont-echo/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 14, 2010 Share Posted August 14, 2010 Either your echo statement is in a program scope where the variables don't exists or the echo statement is inside of some conditional code that is false or the echo statement is inside of some HTML tag and the value is in the 'view source' of the page but does not display or the actual value IS some html tags that don't display when rendered in the browser. It is not possible to debug one line of code taken out of context of how and where it is being used at in the actual program or what values are actually in the data. If you want someone to tell you why your code is doing what it is, you must post all the relevant code and what the data values are that don't display. Quote Link to comment https://forums.phpfreaks.com/topic/210711-array-has-contents-but-wont-echo/#findComment-1099185 Share on other sites More sharing options...
techiefreak05 Posted August 14, 2010 Author Share Posted August 14, 2010 It's probably something with scopes... But the index.php file directly echoes $xconfig['title']; for debugging, and it says it's empty, but when I do the var_dump($xconfig); on the line JUST ABOVE the echo, the results are what I expect. As far as how the others are used... I have a main functions file, called "func.inc.php" and that holds all the function that build the pages (PageTop(), PageBottom(), etc) each file includes the config file, and "func.inc.php", in that order. and the PageTop() function uses the $xconfig values from config.php, but apparently it's not working. example <?php // config.php: $xconfig['title'] = $r['cfg_site_title']; // func.inc.php: function PageTop(){ echo $xconfig['title']; } // index.php: include"config.php"; include "func.inc.php"; PageTop(); //some stuff PageBottom(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/210711-array-has-contents-but-wont-echo/#findComment-1099188 Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 How is the array $xconfig being passed to the PageTop function? Functions have their own variable scope, meaning variables that are defined outside of the function will not be accessible to them (and vise versa). When using functions with variables you should pass them as arguments when you call that function, eg function PageTop(&$xconfig){ echo $xconfig['title']; } // index.php: include"config.php"; include "func.inc.php"; PageTop($xconfig); Documentation on variable scope http://php.net/manual/en/language.variables.scope.php Documentation on functions http://www.php.net/manual/en/language.functions.php Quote Link to comment https://forums.phpfreaks.com/topic/210711-array-has-contents-but-wont-echo/#findComment-1099191 Share on other sites More sharing options...
PFMaBiSmAd Posted August 14, 2010 Share Posted August 14, 2010 I guess you didn't want to solve this anytime today or you would have actually posted the code responsible for the symptom and the values involved. We are not standing right next to you. It is simply not possible to help you with what you see in front of you (your code and the output) without seeing that same information. Quote Link to comment https://forums.phpfreaks.com/topic/210711-array-has-contents-but-wont-echo/#findComment-1099192 Share on other sites More sharing options...
techiefreak05 Posted August 14, 2010 Author Share Posted August 14, 2010 Looks like wildteen88's solution worked. Pass $xconfig as a parameter. Quote Link to comment https://forums.phpfreaks.com/topic/210711-array-has-contents-but-wont-echo/#findComment-1099198 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.