0

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.

1
  • The variable will be set to 0 whenever you include the file, as that's what it's initialised to. If you want to store the changes, you'll need to write the new value out - a database is best, but you could also use a text file. Commented Apr 11, 2013 at 16:45

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.