View Full Version : Why is my Variable Value not carrying through?
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 :)
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.
@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).
Well yea, I guess so... :rolleyes:
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.
Just do this:
<div id="pageheader"><? include($header); ?></div>
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.
Didn't see testingsites post.
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 :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.