What is the value of $b? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

7th Apr 2017, 3:40 PM
Susan Wormley
9 Answers
+ 5
$b = $a++; is identical to $b = $a; $a = $a + 1; So, $b becomes 2 and $a becomes 3.
7th Apr 2017, 4:08 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 3
2
27th Oct 2021, 1:37 PM
Rani Saidah
Rani Saidah - avatar
+ 3
2
7th Dec 2021, 3:31 PM
Medhat Mohamed Ibrahim Abu Mandour
Medhat Mohamed Ibrahim Abu Mandour - avatar
+ 2
first assigns value of a to b, then add ls one to a. that is a postfix.
7th Apr 2017, 3:44 PM
JofS
JofS - avatar
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
11th Sep 2018, 2:49 AM
AKSHAT BHATT
AKSHAT BHATT - avatar
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.
8th Jun 2020, 11:31 PM
Salem Mohamed
Salem Mohamed - avatar
0
2
19th Dec 2020, 9:22 AM
El Maslohi Hassan
0
3
2nd Feb 2023, 10:36 AM
Mohd Bilal
- 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
7th Apr 2017, 4:09 PM
Amjad
Amjad - avatar