23 AnswersNew Answer
hmm, AJ #L1G3 it's tricky though. But here's what I think: (Infix : (A-B++)) => (Postfix : (AB-)++) _____________________________________ <?php $n = 10; $n = $n - $n++; //(($n$n-)++) // $n = 10 - 10 = 0, so in line 3, we have n=0 but remember there's this postfix n++ which makes it 1 (like so (0)++ which is 1) again from 0 in line 3 after subtraction and assignment gets executed echo $n; //If you use the ++ operator as postfix like: var++.The original value of var is returned first then, var is incremented by 1.; ?>
$n++ post increment $n-$n++ original value and after applying post increment original value will be increase by one that's why it will effect on output output is 1
Thanks Rohit,i think i got your explanation
Edward Yes in js will be 0. In PHP I got so many weird things. In PHP $a = 5 echo $a * $a++ will be 30. 😂😂
You should avoid using incremented (decremented) variable more than once between sequence points in languages with C/C++ language incement operator behaviour (like PHP). In Java, expression like int a=1; int b=a++ + a++ + a++; is absolutely valid and predictable (a is incremented right after using its value, Java has no operator overloading). I don't know php standard (php increment operator behaviour is the same as in C++), but according to C++ standard such code causes undefined behaviour since vairable is garanteed to be incremented only at the sequence points (it may be incremented or right after increment operator or later, but before sequence point)!!! $n=10 $n=n-n++ echo $n in C++ such code also gives 1, because (minus) is an operator which is function with operands a and a++ : n=n-n++; is the same as n = operator-(a, a++) The order of evaluation of function operands is not defined, but function call is a sequence point, so operands must be evaluated before function call. On my compiler operands evaluated from right to left (but it is not guaranteed). Right operand a++ is evaluated first, it evaluates to 10 and increments variable a to 11, the left operand now 11. operator-(11, 10) gives 1. $a = 5 echo $a * $a++ will be 30 in C/C++ such code also gives 30, because operator*(a, a++) Right operand a++ is evaluated first, it evaluates to 5 and increments a to 6, the left operand now 6. operator*(6, 5) gives 30. But again, it is code with undefined behaviour. Don't use it!!! Undefined behaviour doesn't mean random result, it means that result of evaluation may be different on other platforms or versions.
Its the after effect ☺
AJ #L1G3 😂😂😂😂
I'm wondering too 🤔(but in js it's 0)
AJ #L1G3 I think js is the strange one where => not a number is a number😎
Ro7ot 😂😂😂😂. Hahaha
Read about php operators it will help you https://www.w3schools.com/php/php_operators.asp
Sanjay Kamath Type casting? What do you mean?
Rahul Thakur Don't advertise your code in QA.
AJ #L1G3 I think he means, "coercion" in js
The lesson here is that don't rely on increments(++) 😂😂😂
i think should be 0. Happening like this 11-10=1. But it includes undefined behavior.. so it may 0 or 1...
It is very simple ✨the variable $n is a post increment variable and the last screen output reflects this...0+1🍺
AJ #L1G3 I think you are missing type casting in js...🤔
AJ #L1G3 (int) rather than var...