+ 17
Why the answer is 1?
It's a challenge question. The answer should be 0 but why the answer is 1? Please check the attached Screenshot https://photos.app.goo.gl/twngE2xh9NQZ17qp8
23 Respuestas
+ 20
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.;
?>
+ 13
$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
+ 10
Thanks Rohit,i think i got your explanation
+ 8
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. 😂😂
+ 8
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.
+ 4
Its the after effect ☺
+ 4
AJ #L1G3 😂😂😂😂
+ 3
I'm wondering too 🤔(but in js it's 0)
+ 3
AJ #L1G3 I think js is the strange one where => not a number is a number😎
+ 3
Ro7ot 😂😂😂😂. Hahaha
+ 2
Read about php operators it will help you
https://www.w3schools.com/php/php_operators.asp
+ 2
Sanjay Kamath Type casting? What do you mean?
+ 2
Rahul Thakur Don't advertise your code in QA.
+ 2
AJ #L1G3 I think he means, "coercion" in js
+ 2
The lesson here is that don't rely on increments(++) 😂😂😂
+ 1
i think should be 0. Happening like this 11-10=1.
But it includes undefined behavior.. so it may 0 or 1...
+ 1
It is very simple ✨the variable $n is a post increment variable and the last screen output reflects this...0+1🍺
+ 1
Sanjay Kamath How it is simple? Just try same logic in JavaScript then see what happens.
+ 1
AJ #L1G3 I think you are missing type casting in js...🤔
+ 1
AJ #L1G3 (int) rather than var...