View Full Version : ? about preincrement/postincrement operators..Please Help TIA

jmhitz

02-17-2010, 06:39 PM

I am just starting out to learn PHP and having difficulty understanding the process i guess...lets say for example:

What would the values of $a__,$b__,$c__, and $d__ be after the following code has run?

$a=10;

$d=$a;

$b=$a++;

$c=++$a;

and then why? Please explain it thoroughly if possible so i understand it.

Thanks for any and all help.

James

Fou-Lu

02-17-2010, 06:48 PM

$a = 12

$d = 10

$c = 12

$b = 10

Starting with $a, since its been incremented twice, once with a post, once with a pre, the result will still be 12 since it started at 10.

$d will be 10. Since $a is not passed by reference, it takes the value that $a had at the time it was introduced. This will result in it being 10.

$b will be 10. $a has been post incremented, so the value assigned to $b will be the value of $a and after this $a increments. Conceptually, $a is 'returned' to $b and then incremented.

$c will be 12 for the exact oposite of why $b is 10. The increment happens first, then the assignment. Conceptually, $a is incremented and then 'returned' to $c. $c is twelve and not eleven since $a has been incremented in the previous operation.

Check this link for some more info: http://php.net/manual/en/language.operators.increment.php

Sorries, give me a sec to update this. I thought the assignments were in order hah

Ok thats better hah

I lied, need to change $c to 12 and why.

JAY6390

02-17-2010, 07:37 PM

++ before a variable will increase the value as the line of code is executed, so

$a = 5;

echo ++$a;

Will output 6

Where as

$a = 5;

echo $a++;

will echo out 5

That said, as soon as that line is executed, both copies will have 6 as the value of $a

jmhitz

02-21-2010, 10:47 PM

So what are the values of $a__,$b__,$c__ and $d__ after this code is run?

$a=5;

$b=++$a;

$c=($b++)-(++$a);

$d=++$c;

would it be...

$a=7;

$b=7;

$c=2;

$d=2;

Thanks again!!!

Fou-Lu

02-22-2010, 03:47 PM

So what are the values of $a__,$b__,$c__ and $d__ after this code is run?

$a=5;

$b=++$a;

$c=($b++)-(++$a);

$d=++$c;

would it be...

$a=7;

$b=7;

$c=2;

$d=2;

Thanks again!!!

Lets see.

The above as equation representatives is:

$a = 5;

$b = 6; // Pre changing $a to 6

$a = 6;

$c = (6 - 7) = -1; // Post changing $b to 7, pre change $a to 7, so $c = -1

$a = 7;

$b = 7;

$d = 0; // Pre changing $c = 0

$c = 0;

So, $a = 7, $b = 7, $c = 0, and $d = 0. Yeah, that looks about right.

If we were to write a pre and post incrementor as a function, it would look something like this:

function preIncrement(&$original)

{

$original += 1; // Add 1 to $original

$result = $original; // Assign $result the value of $original

return $result; // Return the result. If $original was 1, this is now 2.

}

function postIncrement(&$original)

{

$result = $original; // Assign $result the value of $original

$original += 1; // Add 1 to $original;

return $result; // Return the result. If $original was 1, this is 1

}

// And subtract for pre/post decrementers

It doesn't really work this way, but it gives you and idea of what it would look like if it were an actual function.

jmhitz

02-23-2010, 01:19 AM

Thanks.

I just got the $c = (6 - 7) = mixed up.

Now I understand the process.

Thank you very much!!:thumbsup:

James

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.