Variable getting reset before each click

Hello, I have this problem that I cannot get my variable to change more than once. For example, if I press the link that adds 1 to $hey’s value (which is 2 to start with) it then echoes 3. But, if I press it again, it still echoes 3. I got another code with this same problem but a lot more code for other things. I’ve tried to analyze the problem, but I just can’t find the solution :’(

Anyways, here’s the code:


<?php $hey = 2; function runMyFunction1() { echo 'I just ran php function1'; global $hey; $hey++; ?>

    echo "Blev plus 1: ".$hey;

function runMyFunction2() {
    echo 'I just ran php function2';
    global $hey;

    echo "Blev plus 0: ".$hey;

function runMyFunction3() {
    echo 'I just ran php function3';
    global $hey;
    echo "Blev minus 1: ".$hey;

if (isset($_GET['hello'])) {

if (isset($_GET['cow'])) {

if (isset($_GET['ninja'])) {

Run PHP Function +1

Run PHP Function +0

Run PHP Function -1


Thanks in advance


First of all don’t use GLOBAL variables they lead into bad programming habits.

Here’s a way to accomplish what you want without using variables, though I didn’t use functions. However, you can easily do so if you want:


// Don’t use Global Variables they are a bad habit to get into:

if (isset($_GET[‘page’])) { // Check to see if $_GET[‘page’] is set:
$foo = $_GET[‘page’]; // Assigning a silly variable name:
// Validate the page ID:
if (!isset($foo) || !filter_var($foo, FILTER_VALIDATE_INT, array(‘min_range’ => 1))) {
$message = ‘Not A Number’;

/* I used a switch statement and more info can be found at    */
/* */
switch ($foo) :
case 1:
    $message = "I just clicked on Link 1";
case 2:
    $message = "I just clicked on Link 2";
case 3:
    $message = "I just clicked on Link 3";
	case 4:
		  $message = "I just clicked on Link 4";




Untitled Document body { font-size: 100%; } .container { display: block; width: 1100px; height: 550px; background-color: royalblue; margin: 0 auto; } nav { display: block; width: 100%; max-width: 800px; height: 50px; margin: 0 auto; } nav ul { list-style: none; } nav ul li { padding: 0; margin: 0; } nav ul li a { width: 200px; height: 50px; background-color: orange; font-family: Arial, Helvetica, sans-serif; font-size: 1.2em; line-height: 50px; text-align: center; color: #fff; text-decoration: none; padding: 10px; margin: 0; } nav ul li a:hover { background-color: magenta; } h1 { position: relative; top: 100px; width: 800px; font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif; color: #fff; font-size: 2.4rem; text-align: center; margin: 0 auto; }

<?php echo (isset($message)) ? htmlspecialchars($message) : 'Please Click on a Link Above'; ?>


The reason you are encountering the problem is:

each time you load the page to pass the variable it sees $hey = 2
So, each time it resets whatever value is there to 2 and then adds 1 if your condition is met. It’s like a form load event except you are loading the form on each page action sent to the server.

Thanks for opening my eyes to an easier way of getting the same result as I did, but I still have the same problem if I try to insert variables.

Yeah, that much I’ve figured out, but I don’t know how to stop it from reseting. My main code was a calendar that went back and forth between (the value I set +1) and (the value I set -1) when I pressed the button for each action.

Just want to correct a typo - that should had be Global variables instead of just variables…sorry…that would had made it a little clearer.

An while I’m at it this, I hope I even make this clearer by expanding what astonecipher wrote. If you look at the example I wrote:

[php]if (isset($_GET[‘page’])) [/php]

prevents the $foo variable from resetting itself back to the original state before another link is click. This can be true from forms, other input methods or even just calculations among a variety of ways of doing this. The more code you write the more this will sink in and before long it becomes second nature. Using the $_GET, $_POST, $_SESSION statements is a way to “bridge” between the page itself or even between other pages. The if statement, methods (functions) and other programming tools help you prevent variables from resetting.

In short you have to put up some kind of “Wall” or barrier from preventing a variable from resetting or causing an error.


I just don’t understand how you make that wall so that you can change a variable’s value into a value that you have not written yourself in the code. In your example the $foo variable only has the values of undefined (right me if I’m wrong, I just know that it doesn’t have a value before ‘page’ has been set), 1, 2, 3 and 4.

Say that I got a variable(with a value of 2) that is outside the switch and in case 1 I add 1 to it and then echo it. It would echo 3, but what if I’d want to get values that range between 100 and 10.000? I don’t want to make that many links :wink:

Thanks for taking your time to help a beginner :slight_smile:


Read up on GET and POST

Values submitted from a form is usually submitted through POST, and can be received in PHP using the $_POST[‘variable’] superglobal array.

Values submitted via GET is displayed in the address bar, like you can see all over the internet. ie
You can receive the value in PHP using the $_GET[‘variable’] superglobal array.

Superglobal means it is available everywhere. And you can get several values and even arrays using this method.
[php]echo $_GET[‘variable’] . ‘
echo $_GET[‘variable2’] . ‘
echo $_GET[‘finalvariable’] . ‘


In regards to the question about the variable that should increment, you only need to store it somewhere else than your code. Your code is static and can not be changed (easily anyway).

You can store the counter in a file, in a database, in memory, in session, in a GET variable, or however you like. But it must be stored somewhere you can access, increment by one, and read/write.

Thanks to you I’ve now solved the problem! :smiley:
I chose to store the variable in a separate file so that I can load it when I want to change the value and after the change I’ll save it to that file again :slight_smile:

Many thanks, now I can keep working on my school project that’s supposed to be in next week ^^


Sponsor our Newsletter | Privacy Policy | Terms of Service