+ 1

# What is the value of $b?

$a=2; $b=$a++; For this quiz question the answer is 2, but I thought the "++" adds +1 to the number?

9 Answers

+ 5

$b = $a++;
is identical to
$b = $a;
$a = $a + 1;
So, $b becomes 2 and $a becomes 3.

+ 3

2

+ 3

2

+ 2

first assigns value of a to b, then add ls one to a.
that is a postfix.

0

There is a difference between prefix and postfix operator.
prefix operator :: first increase(/decrease) 'a', then update 'b'.
For $a=2 ;
eg.. $b=++$a
First increase $a =3 (i.e. a=a+1)
Now update $b= 3 (i.e. b=a)
Output --> $b= 3
---------------------------------------------------------------
postfix operator :: first update 'b' then increase(/decrease) 'a'.
For $a=2 ;
eg.. $b=$a++
first update $b =2. (i.e. b=a)
Now increase $a= 3. (i.e. a=a+1)
Output --> $b= 2

0

What is the value of $b?
$a=2; $b=$a++;
Answer is :
$b = $a++;
is identical to
$b = $a;
$a = $a + 1;
So, $b becomes 2 and $a becomes 3.

0

2

0

3

- 1

it will be 3 if it is like this:$ a=2; $b=++$a
in your way the b variable will return the value of a to it's normal value