0

why is this php code returning 2 even though its incremented?

<?php $a=2; $b=$a++; echo $b; ?> why does it return 2.. i'm confused..

5th Oct 2018, 1:18 PM
Yohanna Wilbertson
Yohanna Wilbertson - avatar
2 Respuestas
0
That increment placement is postfix, you'll want to use prefix instead. Meaning, $b is assigned to value $a and then incremented after. Writing $b = ++$a will increment before assigning the value and you'll get the result you're looking for.
5th Oct 2018, 2:17 PM
Voxel
Voxel - avatar
0
thanks mate.. i get it now? good explanation there
5th Oct 2018, 2:46 PM
Yohanna Wilbertson
Yohanna Wilbertson - avatar