Page 1 of 1 1
Topic Options
#212924 - 2017-11-21 07:57 PM IIF()
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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?

Top
#212926 - 2017-11-21 08:59 PM Re: IIF() [Re: ShaneEP]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Interesting,

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



Top
#212929 - 2017-11-21 09:58 PM Re: IIF() [Re: Jochen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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])


Edited by ShaneEP (2017-11-21 09:59 PM)

Top
#212931 - 2017-11-22 01:36 PM Re: IIF() [Re: ShaneEP]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
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


Edited by Jochen (2017-11-22 01:38 PM)
_________________________



Top
#212932 - 2017-11-22 05:04 PM Re: IIF() [Re: Jochen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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])

Top
#212933 - 2017-11-22 05:17 PM Re: IIF() [Re: ShaneEP]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
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

Top
#212934 - 2017-11-22 05:48 PM Re: IIF() [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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

Top
#212936 - 2017-11-22 05:54 PM Re: IIF() [Re: Allen]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Yep,
what Shawn Shane said (Still remember the thread back then, but the function nature of iif() got erased in my memory somewhen)


Edited by Arend_ (2017-11-23 10:51 AM)
Edit Reason: ;-)
_________________________



Top
Page 1 of 1 1


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 370 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.063 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.