Why idoes ++ add 2 instead of 1?

what could make the ++ or +1 increment by 2 instead of 1?

I wrote up a counter in PHP but for some reason when I do ++$counter or $counter=$counter+1 it increases by 2. To make things even more confusing I duplicated the counter in the same index file back to back and 1 increments correctly while the other increments by 2.
Why does the site counter increment by 2 when the same code is used in the second counter and it increments by 1 in the same index.php file? When I test the code in its own file it works perfectly.

/* Site Counter */
    $hit_count=0;
    $file_Hcounter ='./sitectr/counter-GLP';
    if (file_exists($file_Hcounter)) {    /* Bump Campaign Counter */
        $hit_count= file_get_contents($file_Hcounter);
    }
    /* Now Save Campaign Count */
    $fc=fopen($file_Hcounter,"w");
    fwrite($fc,++$hit_count);
    fclose($fc);
/* End Site Counter */

/* Campaign Counter */

if ($cp) {
    $cp_count=0;
    $file_counter ='./sitectr/counter_' . $cp;
    if (file_exists($file_counter)) {    /* Bump Campaign Counter */
        $cp_count= file_get_contents($file_counter);
    }
    /* Now Save Campaign Count */
    $fc=fopen($file_counter,"w");
    fwrite($fc,++$cp_count);
    fclose($fc);
}
/* End Campaign Counter */

This?
http://php.net/manual/en/language.operators.increment.php

++$a	Pre-increment	Increments $a by one, then returns $a.
$a++	Post-increment	Returns $a, then increments $a by one.

I know what the ++ is supposed to do but in the first counter the ++ increments by 2 while in the second counter int increments by 1. I want to know why in the same file the 2 counters are not behaving the same even though the code is virtually identicle and they are in the same file back to back as I shown in my post. Also, if I replace the ++$counter with $counter=$counter+1 in the first counter it still increases the counter by 2 not by 1. I am at a loss to explain why it is doing this.

I stab a guess at multiple HTTP requests. e.g. through a redirect.

1 Like

I would add numerous echo '<br>' .__LINE_ ." | " .$incValue; to monitor where the value is changing.

I tried removing the 2nd counter and the first on still increments by 2. Even tried using a different file name to store the counter. I’ve tried changing the variable names in case they may of been reserve words or conflict with the other counter. but it still increments by 2.

yupp, really looks like multiple requests/calls.

1 Like

I’ve tried using echo ($hit_count . ‘
’) and the it looks like it is when I do the ++ or +1

Maybe try

++$hit_count;
fwrite($fc, $hit_count);

I dunno why, but it might be worth trying.

The code is not a procedure call. It is straight line code as you see it. Also, when I remove the 2nd counter the first still counts by 2.

I’ve tried
++$hit_count;
fwrite($fc, $hit_count);

and
$hit_count = $hit_count + 1;
fwrite($fc, $hit_count);

In all 3 cases it increments to counter by 2.

It most likely means that file counter-GLP exists, and file counter_cp (whatever cp is) does not.

My guess is if you echo out $hit_count right before the fopen, it will be equal to 1.

John,
What does LINE do? Is that just the same as a quoted string?

Take a deep breath and read the responses to your question. ++ increments by one. Consider it to be a basic law of the universe right up there with death and taxes. Your code is fine.

The problem is that your counter code is being called twice. No other explanation is possible. That means a redirect is occurring. If you want, post the rest of your html page or a link to it.

1 Like

It echos the line where the variable is displayed.

Did you try adding the script?

[off topic]
To display the double underscore enclose __LINE__ within the character on the top left of most keyboards.
[/off topic]

Edit
All welcome to rephrase the off topic quote.

Is it called a tilde?

ahundiak

The code is straight line, not a procedure.Also the 2 counters are back to back in the same file as I posted it. How does the first counter get called 2 times when the second counter only once? Also, when I try to to trace what is happening with the use of echo I only see it called once not twice. When I tried the following:

echou (‘<br?’ . $hit_count); /* The hit counter equal to n /
$hit_count++;
echou (‘<br?’ . $hit_count); /
The hit counter equal to n+2 */

I saw that $hit_count incremented by 2.

I know that this does not make sense and the code is correct. The 2 counters are the same code. What I don’t know is what else could affect the first counter. Even when I swap the order of the 2 counters the results are he same. I’ve tried renaming the variable to be different from the 2 counters just in case and it still didn’t work. I’d like to know why adding 1 is like adding 2. If these 2 counters are being called twice then why is the second counter not increasing but 2?

Did you see my post? Does the file counter-GLP perhaps exist with the value of 1 in it? You’re reading the file contents into that field, the incrementing it…

Have you tried using autoincrement in a completely unrelated script to see if it works correctly there?

DaveMaxwell
I’ve checked the file counter-GLP and whatever value is there is being incremented by 2, I tried doing
happening with the use of echo I only see it called once not twice. When I tried the following:

echou (‘<br?’ . $hit_count); /* The hit counter equal to n /
$hit_count++;
echou (‘<br?’ . $hit_count); /
The hit counter equal to n+2 */

and the results are very clear that the ++ is 2 not 1; I also tried doing a simple add

$hit_counter = $hit_counter + 1;

and get the same results.

What version of PHP are you using?