如何将变量值从一个php文件更新到另一个php文件


How to update a variable value from one php file to another php file

我有三个php文件在我的项目即"initial.php","inc.php","dec.php"。我在intial.php

中声明了一个变量a

例如:

$every_where=0;

我在另外两个文件中包含了"initial .php",我想增加和减少变量"$ everywhere"的值。所以我所做的是:在"inc.php"

$every_where= $every_where -1;
在"dec.php"

$every_where= $every_where -1;

但是当我从"inc.php"移动到"dec.php"时,它又从0开始,反之亦然。但是我想要一种方法,使$every_where的值在initial.php的每次增加或减少后得到更新。

最简单的解决方案是将值存储在会话中。

在脚本的开头使用:

session_start();
if (!isset($_SESSION['every_where']) {
    $every_where = 0;
} else {
    $every_where = $_SESSION['every_where']
}

然后你可以通过像这样的页面调用来增加和减少这个值:

--$every_where; // in dec.php
++$every_where; // in inc.php

在脚本结束时将值存储回会话:

$_SESSION['every_where'] = $every_where;