1

Here are 2 versions of a very simple class.

Only the getCounterIncrement() function is different in the 2 versions.

Version 1

class Counter {

  protected $counter;

  public function __construct() {

    $this->counter = 0;
  }

  public function getCounterIncrement() {

    return $this->counter++;
  }
}

$counter = new Counter;
print $counter->getCounterIncrement(); // outputs 0
print $counter->getCounterIncrement(); // outputs 1

Version 2

class Counter {

  protected $counter;

  public function __construct() {

    $this->counter = 0;
  }

  public function getCounterIncrement() {

    $this->counter++;
    return $this->counter;
  }
}

$counter = new Counter;
print $counter->getCounterIncrement(); // outputs 1
print $counter->getCounterIncrement(); // outputs 2

Questions

  • Why is the output different in the 2 versions?
  • Is one of the 2 versions better than the other in terms of coding standards?
  • Would there be a nicer / better way to code that?
0

6 Answers 6

5

Standard case of ++ being after or before the variable name. If it comes after the variable name you get the previous value returned as 0 in this case. When you have it after your variable you increment it first and return that new value.

Neither of them is a better or worse way, its just two different operators which can be used in different cases as need be. Post-Increment and Pre-Increment they are called and same for Decrement.

PHP Incrementing/Decrementing Operators

I would probably re-write it a little bit (even though this is no better way) like this

<?php

class Counter {
  protected $counter;

  public function __construct() {
    $this->counter = 0;
  }

  public function incrementValue()
  {
    $this->counter++;
  }
  public function getValue() {
    return $this->counter;
  }
}

$counter = new Counter;
$counter->incrementValue();
print $counter->getValue();

P.S: Looking at your Profile I think you asked this question for fun?

Sign up to request clarification or add additional context in comments.

1 Comment

Hehe... thanks for the very complete answer. And no I didn't ask it just for fun. I just blanked out for a bit on the use of these operators :()
4

From PHP official documentation :

++$a : Pre-increment => Increments $a by one, then returns $a.

$a++ : Post-increment => Returns $a, then increments $a by one.

In your first version, you first return the value, then increment it (post-increment). So if you want to return the incremented value, you have to use the pre-incrementation:

public function getCounterIncrement() {
    return ++$this->counter;
}

Comments

2

In this line you increase the counter after returning the counter:

return $this->counter++;

Instead you need to increase its value before returning, like this:

return ++$this->counter;

Comments

1

When you say for example i++ means use i and then increase it by 1. So in the second version it first increases it and then returns it, that's why the output is different.

Comments

1

Why is the output different in the 2 versions?

Because in first version what is does is, it uses the value first and then increment the same. That's why in first version it print 0,1 and the second version 1,2.

  • In first case it returns the value first and then increment it.
  • In second case it increments the value first and then return it.

Is one of the 2 versions better than the other in terms of coding standards?

No, both are good in terms of coding standard. It all depends on the implementation you want to use.

Would there be a nicer / better way to code that?

It's already a nice way in you've written. It's just about choosing the method you like/want.

Comments

1
  1. Question: ++ after a variable means that the increment will happen after any code execution.

In your 1st option it returns the current value first and then increment.

In your 2nd option it will update in the first line, and in the next line return the updated value.

Hope you understand what I said.

2 and 3 Questions: You can use the ++ before the variable so that it updates and then return the value. But I don't see any problem in your code.

Thanks.

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.