I don't know why am getting confused, can some shine more light to this part? | SoloLearn: Learn to code for FREE!

+7

# I don't know why am getting confused, can some shine more light to this part?

<?php \$a = 10; \$b = \$a++; echo \$a; ?> /* echo \$a, this part I thought it was supposed to output \$a which is 10, why is it outputting "11"? */ /* I also thought that by calling \$b the output will be " 11". */ and also what is the difference between a++; & ++a?

6/13/2017 12:56:41 PM

+20

I hope this could help <?php \$a1 = 10; \$b1 = \$a1++; /*output: \$b2 = 10 and \$a1 = 11 this will pre-increment, get the value and increment \$a1 */ \$a2 = 10; \$b2 = ++\$a2; /*output \$b2 = 11; \$a2 =11; post-increment, this will increment first and place the value */ ?>

+4

\$a = 10; // defines variable a with value 10 \$b = \$a++; //defines variable, post increment var a. echo \$a; //output 11 ++a uses pre increment operator and returns value after increment ( adding 1). a++ uses post increment operator and returns value before increment (original value). b = ++a // y = 10, a = 10 b = a++ //y = 11, a = 10

+4

thanks Lord krishna

+3

a++ means post increment and ++a means pre increment. a++ executes after the statement and ++a executes before the statement. \$b = \$a++ here you are assigning a 10 and after the statement incrementing \$a by one, that's why you get \$b = 10 and \$a = 11

+3

thanks

+2