What is the difference between "++$variable" for "$variable++"?

Asked

Viewed 1,541 times

11

What’s the difference between ++$variavel for $variavel++?

I realize that when I execute a for with both forms, the results are the same.

Example 1:

for ($i = 0; $i < 3; $i++) echo $i;

Prints:

0
1
2

Example 2:

for ($i = 0; $i < 3; ++$i) echo $i;

Prints:

0
1
2
  • So what is the difference between the two forms of incrementation?
  • Adding questions to the question I do not know if it is a very good idea after so many answers.

  • I understand, Mr @Jorgeb.. and removed

  • 1

    Really, it is paia. I had forgotten to ask before. I was going to ask, but I forgot. Maybe I could ask another question :)

4 answers

17


This example you put up is not very good because the variable is always printed after the event that manipulates the variable.

A better example would be like this:

$i = 0;
while ($i < 3) {
    echo $i++;
}

This prints:

0
1
2

And yet:

$i = 0;
while ($i < 3) {
    echo ++$i;
}

This prints:

1
2
3

That is, the first form increments the variable after using the current value, and the second increments before using the value.

  • Also gained a -1 without apparent explanation!

  • @Wallacemaxters Yeah. I wonder what’s wrong with her.

  • I think the last bit of code prints 3 also. Could be the reason for -1.

  • 2

    Well, then it’s corrected.

11

These are increment types, called preincrement ++$i and post-increment $i++.

In the for it is not clear as it is always executed after the execution of the block.

A practical example to see the difference would be:

$i = 1;
echo $i; // 1
echo $i++; // 1
echo $i; // 2
echo ++$i; //3
  • 1

    Good example, friend! This will help a lot who has this doubt

10

You will only find difference if you use this within an expression.

These are pre- and post-increment operators. They are operators that in addition to adding one to the variable’s current value, it changes its value, that is, it is also an assignment operator, producing a side effect.

++$i

is the same as

($i += 1)

or

($i = $i + 1)

Already

$i++

is the same as

$i
($i += 1)

or

$i
($i = $i + 1)

When it is used in isolation, in a statement (which is the case where you used), it makes no difference to use one or the other. But when it is in an expression, the preincrement ($++x) will increment, assign the new value to the variable and it is this value that will be the result of the operation that will be considered in the part of the expression where it was used, while the post-increment ($x++) consider the original value of the variable for use in the expression and only then make the increment, then in the end the value of the variable will be the same, but the value used in the expression will be different:

$i = 0;
echo $i; //imprime 0
echo $i++; //imprime 0. i agora vale 1, o incremento ocorreu depois de usar o valor de i
echo ++$i; // imprime 2 já que primeiro ele fez o incremento, depois usou o valor

In theory the 6 codes below do the same thing, but note that in for and in the first while increment is being used as statement, and it makes no difference, but in the second while when it’s being used as expression, it makes a difference.

for ($i = 0; $i < 3; $i++) echo $i . "\n";
for ($i = 0; $i < 3; ++$i) echo $i . "\n";
$i = 0;
while ( $i < 3) { 
    echo $i . "\n";
    $i++;
}
$i = 0;
while ( $i < 3) {
    echo $i . "\n";
    ++$i;
}
$i = 0;
while ( $i < 3) echo $i++ . "\n";
$i = 0;
while ( $i < 3) echo ++$i . "\n";

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

It makes no difference in the speed to use one or the other.

Some programmers think that there should not be one of them not to confuse. But like everything, it goes from the programmer to know how to use right.

  • +1 by the pre-aincrement and post-increment operators. I always talked to the programming friends "put a 'more' before the variable"

4

They are two forms of increment, pre-fixed and post-fixed. Both do the same thing, that is, increase the variable. A difference can be noted in the example below:

Example of post-fixed:

int x = 1;
int y = 2;

if(y == x++) 
printf("São iguais");
else
printf("Não são iguais");

In this example, the output is: "Não são iguais". For what he does first is to check whether Y is equal X, and not if Y is equal to X++, in the IF it first checks and then increments. It is an important case.

Example of pre-fixed:

int x = 1;
int y = 2;

if(y == ++x) 
printf("São iguais");
else
printf("Não são iguais");

In that case, Y and X will be equal, soon will be printed "São iguais", for he first increases the X and then check with the Y.

  • 3

    Avoid using snippet when not needed, only hinder the response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.