如何使用PHP在另一个PHP脚本中编辑变量? [英] How do i use PHP to edit a variable in another PHP script?

查看:369
本文介绍了如何使用PHP在另一个PHP脚本中编辑变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个基本的PHP应用程序/页面,但是,我遇到了一个问题。我需要制作一个脚本,该脚本将在另一个脚本中编辑变量并将其设置为某些变量。

I am currently in the progress of making a basic PHP app/page, However, I have run into a problem. I have needed to make a script which will edit a variable in another script and set it to something.

例如,一个脚本会说:

$test = "username";

另一个脚本会将变量设置为其他变量。

And the other script would set that variable to something else.

因此,一旦加载了PHP脚本的(index.php)页面,PHP脚本会将第一个脚本(variables.php)更改为:

So, Once the PHP script's (index.php) page is loaded, The PHP script will change the first script (variables.php) to:

$test = "username20";

但是,我不知道该怎么做。我有什么可能的方法?

However, I do not know how to do this. Is there any possible way that I could do this?

编辑:

我需要一个像这样的基本自动编辑器,因为该文件是配置文件(因此,基本上,我需要其中的文本永久存在,这意味着会话将不起作用。)。

I need a basic auto-editor like this because the file is a configuration file (So basically, I need the text inside to be there permanently, Meaning that a "Session" will not work.).

谢谢您的帮助。

推荐答案

您应该使用 fopen 打开文件,用fread读取文件以搜索所需的变量
,然后用fwrite编写。

You should use fopen to open a file, read the file with fread to search the wanted variable and then write with fwrite.

$fn = "variables.php"; 
$file = fopen($fn, "w+"); 
$size = filesize($fn); 
$text = fread($file, $size); 
fwrite($file, 'text'); 
fclose($file); 

这篇关于如何使用PHP在另一个PHP脚本中编辑变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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