Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    ? about preincrement/postincrement operators..Please Help TIA

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    1. $a = 12
    2. $d = 10
    3. $c = 12
    4. $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.op....increment.php

    Edit:
    Sorries, give me a sec to update this. I thought the assignments were in order hah
    Edit:
    Ok thats better hah

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

    Last edited by Fou-Lu; 02-17-2010 at 05:51 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    jmhitz (02-17-2010)

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    ++ before a variable will increase the value as the line of code is executed, so
    PHP Code:
    $a 5;
    echo ++
    $a
    Will output 6
    Where as
    PHP Code:
    $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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    jmhitz (02-17-2010)

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    I think I may be getting it....?

    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!!!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by jmhitz View Post
    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:
    Code:
    $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:
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    jmhitz (02-23-2010)

  • #6
    New Coder
    Join Date
    Mar 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks.
    I just got the $c = (6 - 7) = mixed up.
    Now I understand the process.
    Thank you very much!!

    James


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •