Page 1 of 1
variable assignment inside if-expression?
Posted: 19 Sep 2016 10:33
by autocart
Hi,
This code
Code: Select all
if ($v = 1) {echo $v;}
else {echo "else";}
echos "$v".
This code
Code: Select all
$v = 0;
if ($v = 1) {echo $v;}
else {echo "else";}
echos "0".
This code
Code: Select all
$v = 0;
if ($v) {echo $v;}
else {echo "else";}
echos "else".
I only understand the third one. How does XY handle assignments inside the if expression? (There is no error message.)
Thx.
Re: variable assignment inside if-expression?
Posted: 19 Sep 2016 11:26
by admin
That's not supported.
Tip: Tick "Scripting | Syntax Checking" and you would have been told.

Re: variable assignment inside if-expression?
Posted: 19 Sep 2016 12:06
by autocart
ok, thx for the info
Re: variable assignment inside if-expression?
Posted: 19 Sep 2016 12:39
by PeterH
OK: 2 * autocart, 1 * Don
1) comparison isn't = but ==
2) first example with == would test an uninitialized variable: dangerous!
3) @Don: is it ideal that neither a decimal nor a boolean comparison to an uninitialized var resolves to true, but all to false? While ($v = '$v') is true?
So maybe an uninitialized var *could* be treated as 0 or false. Question: what does meet more expectations

Re: variable assignment inside if-expression?
Posted: 19 Sep 2016 12:42
by admin
Difficult question, but changing this now could break old code, so let's just leave it as it is.
Re: variable assignment inside if-expression?
Posted: 19 Sep 2016 12:45
by PeterH
admin wrote:Difficult question, but changing this now could break old code, so let's just leave it as it is.
Thanks. No problem. Just wanted to ask

Re: variable assignment inside if-expression?
Posted: 19 Sep 2016 14:37
by autocart
PeterH wrote:OK: 2 * autocart, 1 * Don
1) comparison isn't = but ==
2) first example with == would test an uninitialized variable: dangerous!
3) @Don: is it ideal that neither a decimal nor a boolean comparison to an uninitialized var resolves to true, but all to false? While ($v = '$v') is true?
So maybe an uninitialized var *could* be treated as 0 or false. Question: what does meet more expectations

Hi, thx for the input.
Still, for clarification, when I wrote "=" I did mean "=". I was not trying to make a comparision at all but as the title says. I think Don understood me correctly and it is not supported.