PHP在两个文件之间传递变量 [英] PHP Passing variables between two files

查看:156
本文介绍了PHP在两个文件之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码:

foreach (glob('mov/$context.mov') as $filename){ 
    $theData = file_get_contents($filename) or die("Unable to retrieve file data");
}

这是在该glob中添加变量的正确方法吗? $ context

Is that the correct way to add a variable within that glob? $context

另外,在我的新文件中,我想用$ context替换一个单词,所以我会写

Also, in my new file, I want to replace a word with $context, so would I write

$context = "word"; // so it adds that word to the glob function when the script is included in a different file.


推荐答案

你应该在第一个参数中使用双引号 glob 函数。

You should use the double-quotes in the first parameter of the glob function.

glob("mov/$context.mov")

或者,如果你愿意,可以使用括号

or, if you want, use brackets

glob("mov/{$context}.mov")

这样变量名称将替换为值。

In this way the variable name will be replaced with the value.

编辑:

对于另一个问题:

带有 glob 函数的脚本可以多次执行,改变 $ context <的值包含脚本之前的/ code>变量。
示例:


For the other question:
the script with the glob function can be executed multiple times changing the value of the $context variable before the script inclusion. Example:

$context = "word";
include("test.php");

$context = "foo";
include("test.php");

这篇关于PHP在两个文件之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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