using variable in URL in "include" command in PHP

I name an “include” for my PHP page and the location needs to include a variable such as:
include (’/home/httpd/www_mysite_com/htdocs/www.$sld.$tld/.styles.xml’);
When I don’t use the variables in the include like this it works:
include (’/home/httpd/www_mysite_com/htdocs/www.myhomesite.com/.styles.xml’);

However, I’m using this page to feed multiple other pages and I’d really like to include a variable in the specified location for the include. Can this be done? How?

When using single quotes you need to concatenate the variables into the string.

[php]include (’/home/httpd/www_mysite_com/htdocs/www.$sld.$tld/.styles.xml’);[/php]–>
[php]include (’/home/httpd/www_mysite_com/htdocs/www.’ . $sld . ‘.’ . $tld . ‘/.styles.xml’);[/php]

I have also been trying to include a variable inside an include statement. I have tried various versions of the example from JimL above and examples from elsewhere but need more help. I am sure it is just my poor syntax.

This is the original include which works

<?php include "inc-biz-city/biz-intro-city0.html"; ?>

This is my final attempt (trying to edit the version from JimL

$city = “city07”; // variable declared on page

<?php include ("inc-biz-city/biz-intro-".$city.".html"); ?> // edited include

Any help would be appreciated.

ColinK

I don’t see anything wrong with this. Are you getting an error message? (Do you have error reporting enabled?)

This topic is 5 years old. If they haven’t figured it out, they never will. Let it go.

@ColinK actually just posted here and said they were facing the same problem… ^^

That’s when a new thread should be opened.

1 Like

What error are you getting?

Then the forum should close old threads automatically ^^

I am happy to start a new thread. Just need to hear from admin or a moderator.

I assumed time would not be relevant as my issue is nearly identical to the OP, just different variables and file names. Posting here may help the next novice as they will see two similar issues.

Copy /hack/paste developers like me benefit from seeing multiple implementations of similar code :slight_smile:

Thanks for the constructive replies.

I believe error reporting is off, but will check and turn on tomorrow.

The only error I get is that the text in the included file does not load when I use $city vs city0

you’re mixing character sets with quotation marks. Replace the unicode left double and right double quotation marks with ASCII. As:

<?php
$city = "city07";

  include ("inc-biz-city/biz-intro-" . $city . ".html"); 
exit;
?>

your code with mixed quotations:

<?php include (“inc-biz-city/biz-intro-”.$city.".html"); ?>

Now You See it?

1 Like

Thanks johnphpnewb

The " issue was actually a copy error from a text editor which was set up to use windows characters. The actual file did have uft-8 %22 for ". But your reply has encouraged me to change the character set for that editor.

I solved the problem - a silly mistake, but hopefully someone else will get some benefit from the examples and answers here.

My original example (with no $variable) was loading file biz-intro-city0.html but with variable $city07 it was trying to include the non-existent file biz-intro-city07.html

Thanks for the patience form those who replied. Once again phphelp.com is a much more friendly place than stackexchange.com

1 Like

I’m happy that you got it to work. Actually, i allready figured that you forgot to make either the directory or the file. I didn’t mention it yet because if you were using mixed character sets, then you should’ve had atleast a dozen errors displayed by php. I sensed that you were getting frustrated, so we can start from the beginning by correcting the mixed quotes (if you really used them in your code.) If you would’ve replied that it still doesn’t work, then i would’ve asked if you made the directory and the files as specified by the include. I’ve forgotten directories or files before, so this is normal if you are stressed or frustrated.

All is good. Atleast you got it to work. :slight_smile:

PS: I am not a member stackexchange but i use search result material from time-to-time. I don’t like the competitive rate replies and accolades system that they have intact. I like PHPHelp forum. I think that this is the best place to be for PHP Help.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service