Different results for postfix operator (x++) in PHP and JavaScript

Viewed 57

I'm learning web development, and I got puzzled by the strange behavior of a certain code in JavaScript and PHP.

JavaScript

n = 10
n = n - n++
console.log(n) // outputs 0

PHP

$n = 10;
$n = $n - $n++;
echo $n; // outputs 1

I know that, x++ increments x after use and ++x increments x before use. So, n is assigned a value of 0 in second statement, but then will x increase or not?

I guess that it's what making the difference in the outputs, but I'm not sure.

0 Answers
Related