PHP,从另一个php文件获取变量

编程入门 行业动态 更新时间:2024-10-27 04:33:42
本文介绍了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文件获取变量

本文发布于:2023-07-25 12:12:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   文件   PHP   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!