Forum Moderators: coopster

Message Too Old, No Replies

PHP include syntax

can I put a variable inside an include statement?

         

foow

2:30 pm on Jan 3, 2004 (gmt 0)

10+ Year Member



Hi, please forgive this newbie question...

I'm using the following include

<?php include 'includes/I_searchbox.txt';?>

which is fine, but I want to include a variable ($ty_dir) in the include but I cant figure out the correct syntax to make it work

ie say $ty_dir = 'http://www.mysite.com/thisyearsdirectory/';
then I want PHP include that calls
[mysite.com...]
I've tried

<?php include $ty_dir'includes/I_searchbox.txt';?>

and others but can't get it to work. :(
Any help appreciated

[edited by: jatar_k at 5:51 pm (utc) on Jan. 4, 2004]
[edit reason] fixed the bbcodes [/edit]

ikbenhet1

2:34 pm on Jan 3, 2004 (gmt 0)

10+ Year Member



<?php include ( 'includes/I_searchbox.txt?ty_dir='.$ty_dir )?>

This should work. I usually do like this:

$a='includes/I_searchbox.txt?ty_dir='.$ty_dir;
include($a);

foow

3:02 pm on Jan 3, 2004 (gmt 0)

10+ Year Member



thanks kbenhet1, that example didn't seem to work for me but learning from it I tried this

<?php include ( $ty_dir.'includes/I_searchbox.txt' )?> 

and it works! - thanks a million!

ergophobe

7:44 pm on Jan 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



foow,

In case you don't know why... Your last version works because you have put a concatenation operator (.) between your variable and your literal string. That is what makes it work.

One good thing to do if your include doesn't work is to echo it (replace 'include' with 'echo') and see if the string it prints is the name of the file or not.