vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   Community Lounge (https://vborg.vbsupport.ru/forumdisplay.php?f=13)
-   -   (condition ? true : false) and other boolean goodness (https://vborg.vbsupport.ru/showthread.php?t=49872)

filburt1 03-09-2003 10:47 PM

(condition ? true : false) and other boolean goodness
 
It seems like a lot of people don't know about these two gems. The first is inline if. vB for some reason has it's own iif function which works like:
PHP Code:

$a 1;
echo 
iif(($a == 1), "a is one""a is not one"); // echoes "a is one" 

But you can just do this:
PHP Code:

$a 1;
echo (
$a == "a is one" "a is not one"); 

Also as a language construct it should be significantly faster than any user-defined function.


The other one is unnecessary ifs. Check this out:
PHP Code:

function stuff($s)
{
    if (
$s == 50)
    {
        return 
true;
    }
    else
    {
        return 
false;
    }


You can just do this:
PHP Code:

function stuff($s)
{
    return (
$s == 50);


Just sharing the goodness. I don't know why so few people know about these. :confused:

p0wered 03-09-2003 10:56 PM

I was searching similar instructions to develop that sequencer you were helping me with. Php.net is sometimes really hard to understand, because for many functions and stuff like the above do not have sufficient examples which will allow a new user like myself to understand completly the full capabilities of a function or other.

filburt1 03-09-2003 10:58 PM

php.net is designed more as a reference, not a tutorial (although IMO it has the best docs I have ever used for any language). You should use a book instead to learn PHP. This book should rock, judging by I've used a book by the same title except it was for Javascript: http://www.sams.com/catalog/product....-CDF148BBB9A0}

p0wered 03-09-2003 11:07 PM

I go to php.net to 'refer' to the large library of functions in their functions list, I find it that since they made php, they would have everything, of which they do, stuff like boolean and whatnot cannot be found on online resources easily and books, well many people most of the time do not wish to invest in books that won't come in use later.

I for one have an amazing part-time job, and will be checking up on the book you had suggested, and on any others, but til they come, I am stuck with php.net, vB.org and Good ol' filburt :) You seem to know alot!

Velocd 03-10-2003 12:26 AM

I've known about those two techniques, but have had little use for them considering how rarely I use "echo" (since everything is done by templates), and that if I ever make functions, I usually never think about using the comparison operator when returning. ;)

Xenon 03-10-2003 11:59 AM

well, i also know both, the last one is a really good thing i think.

the ?: structure is surely fast, but it makes the code a bit harder to read, so i don't think you should use it in hacks you provied :)

filburt1 03-10-2003 12:01 PM

Personally I don't think it's too hard to read but each to his own. :)

Xenon 03-10-2003 12:17 PM

well, good proggers can read it without problems, but we learn in university not to use that kind of shourtcuts...


All times are GMT. The time now is 01:01 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.01089 seconds
  • Memory Usage 1,733KB
  • 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
  • (4)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)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