PHP,从另一个php文件获取变量 [英] PHP, getting variable from another php-file

查看:741
本文介绍了PHP,从另一个php文件获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想知道当在多个php文件中使用变量名时,是否可以从特定的php文件中获取变量. 一个例子是这样的:

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具有$var1 = 'page1' page2.php具有$var1 = 'page2'

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

footer.php应该具有<a href="">$var1 from page1</a><a href="">$var1 from page2</a>

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?

推荐答案

可以,但是最后一个include中的变量将覆盖第一个中的变量:

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;

输出:

测试

测试器

我建议使用不同的变量名.

I suggest using different variable names.

这篇关于PHP,从另一个php文件获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆