About JavaScript and PHP assignment operators: Why the different results? -
javascript code:
var = 1, b = 2; = b + (b = a) * 0; // result = 2, b = 1;
php code 1:
$a = 1; $b = 2; $a = $b + ($b = $a) * 0; // result $a = 1, $b = 1;
php code 2:
$a = 1; $b = 2; $a = (int)$b + ($b = $a) * 0; // result $a = 2, $b = 1;
what causes difference between php , javascript assignment operators?
is operator precedence related?
i want know reasons are. thanks!
no, operator precedence doesn't factor evaluation order, therefore using assignments in complex evaluations reusing result of evaluation undefined.
operator precedence , associativity determine how expressions grouped, not specify order of evaluation. php not (in general case) specify in order expression evaluated , code assumes specific order of evaluation should avoided, because behavior can change between versions of php or depending on surrounding code.
in short, output of $b + ($b = $a)
undefined, since grouping overrides precedence, , not in anyway enforce whether assignment occurs before fetching left operand of addition or after. precedence defined, execution/evaluation order not.
Comments
Post a Comment