Jump to content

Include() and Variables


watagal

Recommended Posts

greetings,I'm trying to create an include file that contain variable definitions I use throughout my site, yet when I include this file on other pages (files) - I don't get the variables.Here's my "test.inc" file:

<?php    $test1 = "This";    $test2 = "is a";    $test3 = "test";?>

Next page:

<?php include "test.inc"; ><html><head>....<body><p><?php echo $test1 $test2 $test3 ?></p></body>

Can I do this? How?Thanks, WG

Link to comment
Share on other sites

...you have some things wrong :)"next page"

<?php include('test.inc'); ><html><head>....<body><p><?php echo $test1 $test2 $test3; ?></p></body>

also, if the include is in another folder, say http://site.com/files/other, and new page is in http://site.com/files you need to have the include be include('other/test.inc');

Link to comment
Share on other sites

Using <?php include "test.inc"; > is not wrong.<?php echo $test1 $test2 $test3; ?> is wrong. But probably it was made when typing the post.See if the test.inc is in the same folder as the script (as reportingsjr said), see the error log for warnings/errors if they are not shown on the page.Btw, if I would just type in the browser "www.site.com/test.inc" I would see all your code! Rename it to test.inc.php for example.

Link to comment
Share on other sites

Using <?php include "test.inc"; > is not wrong.<?php echo $test1 $test2 $test3; ?> is wrong. But probably it was made when typing the post.See if the test.inc is in the same folder as the script (as reportingsjr said), see the error log for warnings/errors if they are not shown on the page.Btw, if I would just type in the browser "www.site.com/test.inc" I would see all your code! Rename it to test.inc.php for example.

how is <?php echo $test1 $test2 $test3; ?> wrong? it needed to have the semicolon to define that it stopped in the first one..
Link to comment
Share on other sites

If you are using echo to print multiple things, you should do it one of two ways:echo $test1, $test2, $test3;echo $test1 . $test2 . $test3;Also, this part is wrong, because it's missing the closing tag:<?php include "test.inc"; ><?php include "test.inc"; ?>

Link to comment
Share on other sites

It is actually quite simple. Variables contain strings or integers, and in most cases they are strings. Strings can be concatenated, using the dot (.) and the instruction "echo" can also output several variables, then using the comma (,). So in case of (only) the echo instruction, outputting several strings, variables, integers or combinations, it would be legal to use either the dot or the comma :)I don't know about the shoirt notation of the PHP tags, but sure the end tag should end with ?> I thought, I agree with justsomeguy.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...