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

11

What is the difference between ++$variavel to $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's the difference between the two ways of incrementing?
asked by anonymous 12.08.2015 / 17:16

4 answers

16

This example you put in is not very good because the variable always prints after the event that handles the variable.

A better example would look 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 one increments before using the value.

    
12.08.2015 / 17:19
11

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

No for is not clear because it is always executed after block execution.

A practical example to see the difference would be:

$i = 1;
echo $i; // 1
echo $i++; // 1
echo $i; // 2
echo ++$i; //3
    
12.08.2015 / 17:22
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 current value of the variable, it changes the value of it, that is, it is also an assignment operator, producing a side effect.

++$i

is the same as

($i += 1)

or

($i = $i + 1)

Now

$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), it does not make any difference to use one or the other. But when it is in an expression, the pre-increment ( $++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 that the post-increment ( $x++ ) will consider the original value of the variable for use in the expression and only then increment, then at 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 first while the increment is being used as statement , and makes no difference, but in the second while when it's being used as an expression, 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";

See running on ideone .

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

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

    
12.08.2015 / 17:22
4

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

Postfix example:

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 it does first, is to check that Y is equal X , and not Y is equal to X++ , in IF it first checks and then increments. This 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 this case, Y and X will be equal, so it will be printed "São iguais" , since it first increments X and then checks with Y .

    
12.08.2015 / 17:44