ShaneEP
(MM club member)
2017-11-21 07:57 PM
IIF()

This may or may not be considered a bug. But it seems weird to me though, so figured I would bring it to light to see what others think.

It appears that IIF() evaluates data in all blocks, instead of just the block that should be returned.

Example...
 Code:
$array = '1','2','3'
$test = IIf(1, $array[0], $array[3])
This fails because array[3] is out of bounds, even though in my opinion it should only evaluate the code that says array[0] and ignore the other block.

I ran across this issue, trying to golf down some code, so it probably wouldn't be an issue for most uses. But what do you guys think?


JochenAdministrator
(KiX Supporter)
2017-11-21 08:59 PM
Re: IIF()

Interesting,

I‘d rather have it failing my code for sanity‘s sake than stabbing me in the back on runtime though


ShaneEP
(MM club member)
2017-11-21 09:58 PM
Re: IIF()

But there are legitimate uses for such logic...Let me expound on the example a bit..

If $x is 5 or less it works, but will cause error when above 5, even though the second block would function.
 Code:
$array = '0','1','2','3','4','5'

$x = 50

$test = IIf($x<6, $array[$x], $array[$x mod 10])


JochenAdministrator
(KiX Supporter)
2017-11-22 01:36 PM
Re: IIF()

Still,
checking sanity of array boundaries is one thing I wouldn't want to have ommited.
As $x is 'variable' in most use cases and not 'constant' \:\)
Let's hear the others out though


ShaneEP
(MM club member)
2017-11-22 05:04 PM
Re: IIF()

I agree with what you're saying. But in my mind, IIF() should act exactly like an If, Else, Endif. And in currently doesn't.

If this code works without error...
 Code:
$array = '0','1','2','3','4','5'
$x = 51
If $x < 6
   $test = $array[$x]
Else
   $test = $array[$x mod 10]
Endif
Then the IIF equivalent should as well, in my opinion.
 Code:
$array = '0','1','2','3','4','5'
$x = 51
$test = IIf($x < 6, $array[$x], $array[$x mod 10])


AllenAdministrator
(KiX Supporter)
2017-11-22 05:17 PM
Re: IIF()

I was thinking this has/had come up before... see if this helps.
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=79397#Post79397


ShaneEP
(MM club member)
2017-11-22 05:48 PM
Re: IIF()

Yeah, I guess that makes sense. Since it passes to a function, it always gets evaluated. So not really a bug.

At least I wasn't the only one to think it was weird! lol


JochenAdministrator
(KiX Supporter)
2017-11-22 05:54 PM
Re: IIF()

Yep,
what Shawn Shane said (Still remember the thread back then, but the function nature of iif() got erased in my memory somewhen)