Prashant
Prashant

Reputation: 71

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

I have three php files in my project namely "initial.php","inc.php","dec.php".I declared a variable a in intial.php

For example:

$every_where=0;

I have included "intial.php" in my other two files and I wanted to increment and decrement the value of variable "$everywher".So what i did is: In "inc.php"

$every_where= $every_where -1;

In "dec.php"

$every_where= $every_where -1;

But when I move from "inc.php" to "dec.php" it starts from 0 again, and vice-versa. But wanted a way so that the value of $every_where gets updated after every increment or decrement in initial.php.

Upvotes: 0

Views: 1492

Answers (1)

insertusernamehere
insertusernamehere

Reputation: 23580

The most simple solution would be to store the value in the session.

In the beginning of your script use this:

session_start();

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

Then you can increment and decrement the value through page calls like this:

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

At the end of your script store the value back to the session:

$_SESSION['every_where'] = $every_where;

Upvotes: 3

Related Questions