variable assignment inside if-expression?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
autocart
Posts: 1391
Joined: 26 Sep 2013 15:22

variable assignment inside if-expression?

Post 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.

admin
Site Admin
Posts: 66677
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: variable assignment inside if-expression?

Post by admin »

That's not supported.

Tip: Tick "Scripting | Syntax Checking" and you would have been told. :)


PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: variable assignment inside if-expression?

Post 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 :ugeek:

admin
Site Admin
Posts: 66677
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: variable assignment inside if-expression?

Post by admin »

Difficult question, but changing this now could break old code, so let's just leave it as it is.

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: variable assignment inside if-expression?

Post 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 :maf:

autocart
Posts: 1391
Joined: 26 Sep 2013 15:22

Re: variable assignment inside if-expression?

Post 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 :ugeek:
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.

Post Reply