Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

PHP, getting variable from another php-file

So I wonder if it is possible to get a variable from a specific php-file when the variable-name is used in multiple php-file. An example is this:

<header>
 <title>
  <?php echo $var1; ?>
 </title>
</header>

page1.php has $var1 = 'page1' page2.php has $var1 = 'page2'

footer.php should have <a href="">$var1 from page1</a><a href="">$var1 from page2</a>

Ok the example is a bit abstract, but as short as I can make it. I think you get what I am getting at! So it is the in the footer I am after! Got any solutions?

question from:https://stackoverflow.com/questions/13135131/php-getting-variable-from-another-php-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can, but the variable in your last include will overwrite the variable in your first one:

myfile.php

$var = 'test';

mysecondfile.php

$var = 'tester';

test.php

include 'myfile.php';
echo $var;

include 'mysecondfile.php';
echo $var;

Output:

test

tester

I suggest using different variable names.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...