Jump to content

Array has contents, but won't echo...


techiefreak05

Recommended Posts

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  :psychic:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.