vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   iif Function - what's using for ? (https://vborg.vbsupport.ru/showthread.php?t=74611)

Creative Suite 01-18-2005 08:48 PM

iif Function - what's using for ?
 
well , i want to have any easy example with explaining for ( iif ) function , please :)

regards ;)

Xenon 01-18-2005 08:55 PM

for asigning variables?

as an example
PHP Code:

$navbar iif(isloggedin(), fetch_user_nav(), fetch_guest_nav()); 

it can just speed up processes :)

Andreas 01-18-2005 09:03 PM

It's "a shortcut" for

PHP Code:

if (isLoggedin()) {
  
$navbar fetch_user_nav();
} else {
  
$navbar fetch_guest_nav();


Or in other words
PHP Code:

$navbar isLoggedin() ? fetch_user_nav() : fetch_guest_nav(); 


Creative Suite 01-18-2005 09:53 PM

Xenon & KirbyDE

thanks , i understood it :)

regards :classic:

Xenon 01-18-2005 09:55 PM

you're welcome :)

sabret00the 01-18-2005 09:55 PM

i think it's official name is inline if conditional, not sure though :)

Marco van Herwaarden 01-18-2005 10:01 PM

aka Inline If

Xenon 01-18-2005 10:03 PM

Insert If?

:D

Jolten 01-18-2005 11:17 PM

Terniary operator I believe.

Xenon 01-19-2005 11:51 AM

yep, that's the general terminus for the ? : construct

WetWired 01-19-2005 01:13 PM

Does PHP not directly support ?: ?

sabret00the 01-19-2005 01:19 PM

it does but the IIF's are purdier ;)

Andreas 01-19-2005 01:27 PM

Quote:

Originally Posted by WetWired
Does PHP not directly support ?: ?

It does. iif() is just a wrapper.

filburt1 01-19-2005 02:22 PM

In iif(), the third argument is optional, which can make it more convenient every now and then.

Xenon 01-20-2005 09:17 PM

not that i ever needed that special behaviour of iif ;)

deathemperor 01-21-2005 09:57 AM

iff() is really useful when used within queries, it's such a simple but effective functions. Vb staffs always do great jobs.

sabret00the 01-21-2005 12:31 PM

isn't IIF actually a php function?

guess not, i've been using it all this time and didn't even know lol

Andreas 01-21-2005 12:34 PM

No, it's a vBulletin function defined in functions.php

deathemperor 01-21-2005 12:50 PM

Quote:

Originally Posted by sabret00the
isn't IIF actually a php function?

guess not, i've been using it all this time and didn't even know lol

that's really funny, guess you use editor with non color highlight ? if DW or any other that highlight php functions they won't do in iff(), simple it's not a php based

sabret00the 01-21-2005 02:49 PM

Quote:

Originally Posted by deathemperor
that's really funny, guess you use editor with non color highlight ? if DW or any other that highlight php functions they won't do in iff(), simple it's not a php based

you know what, i've only just realised that it don't highlight the iif() in the red it uses for other functions. damn. lol

Dean C 01-21-2005 03:22 PM

The function is kind of redundant, the only time it helps is with clarity for new coders. I just tend to use in my scripts :)

PHP Code:

$var $arewetrue 'yes' 'no'


Brad 01-21-2005 04:19 PM

Quote:

Originally Posted by KirbyDE
No, it's a vBulletin function defined in functions.php

Heres the code for it if anyone dos'nt want to go searching.

From functions.php

PHP Code:

// ###################### Start iif #######################
function iif($expression$returntrue$returnfalse '')
{
    return (
$expression $returntrue $returnfalse);



Xenon 01-21-2005 04:36 PM

actually my editor highlights it as well, as i'm highlighting some vb functions ^^

noppid 01-21-2005 06:07 PM

iif() is handy for true and false returns. However if you use it to test and then pass data, if the data chunk is large, it may be much more inefficent then a plain old if/else.

filburt1 01-21-2005 08:17 PM

I did just notice one major problem with it, but it's the design of PHP (and most other languages) that causes it: the ternary operator supports lazy evaluation (only evaluates what is needed based on the condition), whereas the iif() function evaluates all of its arguments regardless of the condition's value.

An excerpt of my code that demonstrates the problem:
PHP Code:

function vbms_get_all_quota_usages($userids false)
{
    global 
$DB_site;
    
    
$usages = array();    
    
$extrawhere iif($userids !== false"AND userid IN (" implode(", "$userids) . ")"); 

Look at that last line. If $userids strictly equals false, it will still try to implode $userids, and of course you can't implode a non-array. The ternary operator's lazy execution doesn't try to implode $userids if it is boolean false.

noppid 01-21-2005 08:43 PM

Ah yes and here's the example I had written and realized the overhead it created just moving a copy of the data, I overlooked that it executed the addslashes() function as well even if it was not needed.

This is very inefficient, in some cases $data could have been 1MB...

PHP Code:


iif
($vbg_use_fs''addslashes($data)); 

Nice tip filburt.

deathemperor 01-22-2005 03:37 AM

as the name of the varibles vb defined, this is absolutely useful for boolean vale the best.


All times are GMT. The time now is 11:28 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04664 seconds
  • Memory Usage 1,772KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (7)bbcode_php_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (27)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete