Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a cookie where a number is stored. When I want to add "1" to the stored number and then overwrite it, I use this php code:

$temp = $_COOKIE['mycookie']++;
$_COOKIE['mycookie']=$temp;

Is it the best way to do that?

share|improve this question

closed as off-topic by Emily L., syb0rg, 200_success Jun 17 at 15:58

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – 200_success
  • "Questions must involve real code that you own or maintain. Questions seeking an explanation of someone else's code are off-topic. Pseudocode, hypothetical code, or stub code should be replaced by a concrete example." – Emily L., syb0rg
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
You have edited to say your code is working, yet, in its current form, it can't be. $temp will be the value of the cookie prior to the post-increment is applied, and you are then overwriting the cookie with the unchanged value. i.e. you save the cookie value to temp, the you increment it, then you restore the cookie value to what it was before the increment. That is what your code does, which is not what you are describing that you actually get. There is a discrepency. –  rolfl Jun 17 at 17:37
    
You guys were right, the code is broken. Ignore the votes to open as his code does not do what he says it does. Looks like @rolfl beat me to it –  Alex L Jun 17 at 17:40
    
That's weird, I was pretty sure it worked. Maybe I remembered of an other code I had written which was similar and worked. I really apologize, I should've not trusted of my memory and tested again the code before posting my question. –  Angelo Jun 17 at 17:52

3 Answers 3

up vote 5 down vote accepted

I think what you may be looking for is found on this page.

This is happening because of the ++ being used as a post-increment instead of a pre-increment. Essentially what is happening is you're saying, "set $cookie to the value of $_COOKIE['count'], and then increment $_COOKIE['count'].

Go for ++$_COOKIE['mycookie']; instead and let us know how that works out.

share|improve this answer
    
Thank you, now it works! –  Angelo Jun 17 at 15:35
    
Answer better than mine. Well done. –  Madara Uchiha Jun 17 at 15:35

I'm certainly no expert in PHP, but in most languages I've used (and it seems PHP is no exception) there's a += operator which seems like it would be better for this situation:

$_COOKIE['mycookie'] += 1
share|improve this answer

There's nothing wrong with just

$_COOKIE['mycookie']++;

Note that this answer is incorrect. See the accepted answer.

share|improve this answer
    
Thanks for your answer, but it doesn't work for me. I've tried to launch more times the code <?php $plus = $_COOKIE['TestCookie']++; setcookie("TestCookie", $plus , time()+3600, "/"); ?> but the value of my cookie remains the one I had set before –  Angelo Jun 17 at 15:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.