Quiz query, $d.$d--=20?
$d = 5; echo $d*$d--; The answer I believed was 25 but apparently it is 20. To try debugging, I did: echo $d.$d--; And I get 45. 5*4 alone is confusing for post decrement. Why is it 4*5? Can someone explain? Thanks!
4/5/2019 10:25:42 AM👑 Prometheus 🇸🇬
10 AnswersNew Answer
The $d-- is being evaluated first so it's actually 4 * 5. I tried to find this is the documentation and it seems this is undefined behavior so I wouldn't rely on it always working this way.
Ipang you did it right, it took me a while too, to figure that out in C
Ipang maybe test em all the time before use, in a different language, with time you won't need that again
This is really odd, we commonly understood postfix operator doesn't change the operand's value at least until the next statement line was executed. It's funny how PHP treats postfix & prefix operator the same. I'm looking forward to hearing something about this odd behaviour. $d = 5; echo '$d * $d++ = ' . $d * $d++ . '<br>'; // output: 30 $d = 5; echo '$d * ++$d = ' . $d * ++$d . '<br>'; // output: 36 ?>
Ipang I think it's working from right to left just like in C when you put such in a printf function ⚡Prometheus ⚡ this is probably the case
*AsterisK* I'm having difficulty when it comes to evaluating expressions, especially now that I'm aware of this various operator associativity evaluation from different languages, wish there was a way to easily remember which language does it how 😁
*AsterisK* Good idea, I'll keep a note on this for future. Thanks mate 👍