Log in

View Full Version : Why is my Variable Value not carrying through?



kuau
02-03-2008, 11:30 PM
I have a tiny file that sets some variables...


<? $artist_id = 46;
$header = 'artist-header-nav.php';
include_once('artist-template-n.php'); ?>

and then runs a php script that uses the variables. For some reason the $header variable isn't recognized in the template file...


<div id="pageheader"><? include('$header'); ?></div>

What am I missing? Thanks! erin :)

Nile
02-04-2008, 12:49 AM
I think you would need to do this:

<div id="pageheader"><? include($header); ?></div>

thetestingsite
02-04-2008, 12:57 AM
If that doesn't work, try to simply echo the variable $header to see if it is getting passed through to the script.

Hope this helps.

Nile
02-04-2008, 01:01 AM
@thetestingsite
It will defently work. lol.

thetestingsite
02-04-2008, 01:03 AM
@nile, I know it should work; however, there are some cases in which that just isn't the case. It is always nice to have a backup plan when troubleshooting something (especially in a case like this).

Nile
02-04-2008, 01:22 AM
Well yea, I guess so... :rolleyes:

kuau
02-04-2008, 01:32 AM
You guys kinda lost me a bit. I tried this... is that what you meant? Anyway, it didn't work.


<? include('echo $header.".php";'); ?>

Also tried this... and it didn't work.


<? include('echo $header;'); ?>

thetestingsite
02-04-2008, 01:35 AM
Actually, what I suggested was if this doesn't work:



<?php include($header); ?>


then try this instead to see if the variable is getting passed:



<?php echo $header; ?>


Hope this helps.

Nile
02-04-2008, 01:36 AM
Just do this:

<div id="pageheader"><? include($header); ?></div>

kuau
02-04-2008, 01:50 AM
I did try echoing the variable and it gave me the correct filename. So then I did what Nile suggested:


<? include($header); ?>

and it worked!! Thanks so much.

What I don't understand is this... if I were not using variables, I would do the command like this:


<? include('artist-header-nav.php'); ?>

Replacing the variable above, it ends up being:


<? include(artist-header-nav.php); ?>

So does that mean I shouldn't (or don't have to) use the single quotes normally? That seems to be what was making it not work.

Thanks so much for helping me. erin :)

thetestingsite
02-04-2008, 01:55 AM
ok, basic PHP tutorial:

when using include or require functions, if you do something like this:



include('$header');


it looks for the file name $header; however, if you do this:



include($header);


it then includes the file that is assigned to the variable $header.

Hope this helps.

Nile
02-04-2008, 03:09 AM
Didn't see testingsites post.

kuau
02-04-2008, 05:17 AM
Nile, I just realized that your "signature" confused me - I thought it was the answer to my question so I tried to apply it to my code -- sorry, you must have wondered what the heck I was doing.

TTS, I understand that the quotes turn it into a literal. I am just curious that the result seems to be bad syntax because when the variable is replaced, you end up with:


include(filename.php);

instead of:


include('filename.php');

I thought using the quotes around filename.php was proper syntax. But I'm not sure.
Thanks, erin :)