Page 1 of 1

Scripting Error with Nested 'If'

Posted: 11 Nov 2011 13:13
by PeterH
The result of the following code is wrong!
It's a simplified excerpt from real code, just to test/show the problem.
(Note: $b *could* be '', too.)

Code: Select all

  $a = '';
  $b = 'b';
  
  if ($a = '')
  { $a = $b;
    if ($a = '') {$a = 'cc';}
  }
  
  Msg $a; 
The first If correctly recognizes 'True' and executes it's conditional code: OK.
The second If correctly recognizes 'False', but nevertheless executes it's conditional code: that's wrong!

"Stepping" shows for the condition of the 2nd If
b = ''
but then the conditional
$a = 'cc'
is wrongly executed!

In the moment when stepping shows
$a = 'cc';
you can look for Variables: $a still contains 'b'! Then it's overwritten.

Re: Scripting Error with Nested 'If'

Posted: 11 Nov 2011 13:31
by Stefan
Is this an typo just now
or did you really use
if ($a = '')

instead of
if ($a == '')


?


See Help > Advanced Topics > Scripting > If/ElseIf/Else Blocks
Examples:

Code: Select all

// shows "else", "elseif", "if"
 $i = 1;
 while ($i) {
   if ($i == 3) {
     echo "if";
     break 2;
   }
   elseif ($i == 2) {
     echo "elseif";
   }
   else {
     echo "else";
   }
   $i++;
 }

Re: Scripting Error with Nested 'If'

Posted: 11 Nov 2011 13:54
by highend

Code: Select all

  $a = '';
  $b = 'b';
  
  if ($a == '')
  { $a = $b;
    if ($a == '') {$a = 'cc';}
  }
  
  Msg $a;
Works as expected so you probably made the error of assigning a value in your if statements instead of comparing it with a variable.

Edit: Oh sorry, Stefan answered it already (had to do something else in the meantime while my answer was open in another browser tab).

Re: Scripting Error with Nested 'If'

Posted: 11 Nov 2011 14:42
by PeterH
:oops: You are right...

Seems when you have programmed in one language you should never ever try to use another - you always run the risk of mixing up the syntax...
(...and it wasn't only one for me :roll: )
OK: one exception might be assembler 8) - no big risk to mix 'em up...

Re: Scripting Error with Nested 'If'

Posted: 11 Nov 2011 18:44
by Trasd
PeterH wrote::oops: You are right...

Seems when you have programmed in one language you should never ever try to use another - you always run the risk of mixing up the syntax...
(...and it wasn't only one for me :roll: )
OK: one exception might be assembler 8) - no big risk to mix 'em up...
Don't feel bad, I've stared at code for days only to remember I was using another's syntax! It's all part of the fun...