New course! Every coder should learn Generative AI!
Try a free lesson0
Can someone better explain increment/decrement operators?
ok so i know that $var++ means $var=$var+1 but if you like say: $var1=1; $var2=$var1++; why is it turning into var1 having the value of 2 and var 2 having the value of 1? is it assigning the value of var1 to var2, then increments it?
2 Answers
+ 2
yes, correctly.
+ 1
$var2=$var1++;
here 2 operators are used one is assignment (=) which is used to assign the value of var1 to var2 and then second operator(++) is used to increment the var1 value.
there are 2 types of increment operator
1. post increment(var++) which will increment after the operation completed.(means first assign the value then increment)
2.Pre increment(++var) which will increment first and then complete the operation