684425 Posted January 18, 2015 Share Posted January 18, 2015 <?php $i = 2; $i = $i++; echo $i; ?> Output of above code is 2. <?php $i = 2; $p = $i++; echo $i; ?> Output of above code is 3. Why this difference? I mean how these codes are processed to give an output? Quote Link to comment https://forums.phpfreaks.com/topic/294024-why-difference-in-outputs/ Share on other sites More sharing options...
Ch0cu3r Posted January 18, 2015 Share Posted January 18, 2015 $i++ increments the value after the (assignment) operation has occurred, not during the (assignment) operation. For that you need to use ++$i But there is no need to do $i = $i++; just do $i++; Quote Link to comment https://forums.phpfreaks.com/topic/294024-why-difference-in-outputs/#findComment-1503304 Share on other sites More sharing options...
684425 Posted January 18, 2015 Author Share Posted January 18, 2015 Actually the question was asked in a facebook group. and my answer there was... You ignored variable $p in second code and directly called an output of $i so it became $i = 2; $i++; for php. In your 1st code you are trying to do the same thing, but because of only one variable $i on both sides in 2nd line, php ignored #YOU and did it work. 1st code $i = 2; //declared an assigned a value. $i = $i++; //another declaration. ------------------------------------ 2nd code $i = 2; //declared an assigned a value. $i++; // direct increment. Still i was confused... Thanks dear for reply Quote Link to comment https://forums.phpfreaks.com/topic/294024-why-difference-in-outputs/#findComment-1503305 Share on other sites More sharing options...
Barand Posted January 18, 2015 Share Posted January 18, 2015 To make your second code equivalent to the first you would echo $p. <?php $i = 2; $p = $i++; // value assigned to $p then $i is incremented echo $p; //--> 2 echo $i; //--> 3 ?> Quote Link to comment https://forums.phpfreaks.com/topic/294024-why-difference-in-outputs/#findComment-1503312 Share on other sites More sharing options...
684425 Posted January 23, 2015 Author Share Posted January 23, 2015 Explained by a friend. $i = $i++; // internally is more like $temp = $i++; // $temp = 2; $i=3 $i=$temp; // $i=2 Quote Link to comment https://forums.phpfreaks.com/topic/294024-why-difference-in-outputs/#findComment-1503966 Share on other sites More sharing options...
scootstah Posted January 23, 2015 Share Posted January 23, 2015 (edited) Explained by a friend. $i = $i++; // internally is more like $temp = $i++; // $temp = 2; $i=3 $i=$temp; // $i=2 That doesn't seem to be explaining anything. $i = 2; $p = $i++;When you say this, you are assigning $p to the value of $i before $i is incremented. So since the value of $i is 2, $p is equal to 2. After $p is assigned to the value of $i, only then does $i get incremented. On the next statement, $i is equal to 3, since it has then been incremented. Now, when you use the same variable, you are basically overriding the would-be incremented value. So if you say $i = 2; $i = $i++;you are setting $i to the value of $i again before it is incremented, which is 2. Since you're using the same variable name, you're effectively preventing the increment from happening. But if you changed it to ++$i, then you would end up with 3. $i = 2; $i = ++$i; echo $i; // $i = 3This is because, the increment is evaluated before the assignment happens. So by the time $i gets its new value, the increment has happened and the new value is 3. Hopefully that all came out in a way that makes sense. EDIT: This is kind of what is happening internally when you do $i++ vs $++i. // -- $p = $i++; $i = 2; $p = $i; // $i == 2; $p == 2; $i = $i + 1; // $i == 3; $p == 2; // -- $p = $++i; $i = 2; $i = $i + 1; // $i == 3; $p = $i; // $i == 3; $p == 3; Edited January 23, 2015 by scootstah Quote Link to comment https://forums.phpfreaks.com/topic/294024-why-difference-in-outputs/#findComment-1503969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.