vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Syntax: checking if multiple variables are set (https://vborg.vbsupport.ru/showthread.php?t=114883)

lightnb 05-06-2006 02:42 AM

Syntax: checking if multiple variables are set
 
I want a bit of code to run if $HD is set, $VD is also set, and $TD is NOT set.

Is there an easy way to do this?

I'm guessing it would be somthing like:

PHP Code:

if( isset( $GT2 and $VD !and $TD) ) {solve for $TD, and set $TD the answer}; 

Th PHP manual only gives an example of isset with one variable. Any help would be appreciated.

thanks,

Nick

Paul M 05-06-2006 02:50 AM

More like ;

PHP Code:

if ( isset($GT2) and isset($VD) and !isset($TD) ) 


lightnb 05-06-2006 03:03 AM

Thanks :)

Is this valid code to set $C equal to $A squared plus $B squared?

PHP Code:


if ( isset($A) and isset($B) and !isset($C) ) {$C=sqrt(($A*$A)+($B*$B));} 


Paul M 05-06-2006 03:11 AM

Um, did you mean set $C to the Square Root of $A squared plus $B squared ;)

lightnb 05-06-2006 03:12 AM

yeah, that was a typo...

I'm trying to calculate pythagreom therum. (sp?)

Paul M 05-06-2006 03:15 AM

Then yes, it looks valid.

Adrian Schneider 05-06-2006 03:18 AM

Yep! Personally I would pow(X, 2) to square it, but that doesn't matter. I gave it a trial run with 3, 4, and 5:
PHP Code:

<?php

$A 
3;
$B 4;

if (isset(
$A) and isset($B) and !isset($C))
{
    
$C sqrt(pow($A2) + pow($B2));
}

echo 
$C;

?>

Both methods gave the desired result of 5.

lightnb 05-06-2006 03:21 AM

Thanks :)

Do you know if there's a command to check and see if any two variables out of a whole bunch of them isset?

Adrian Schneider 05-06-2006 03:34 AM

I highly doubt there is, but here is a quick one I wrote:

PHP Code:

<?php

function atLeastTwo($values)
{
    
$counter 0;
    foreach (
$values as $value)
    {
        if (
$value !== null)
        {
            
$counter++;
            if (
$counter == 2)
            {
                return 
true;
            }
        }
    }
    return 
false;
}

$a 1;
$b 2;

$values = @array($a$b$c);

if (
atLeastTwo($values))
{
    echo 
"Woot!  At least two are set!";
}
else
{
    echo 
"Phooey!  Less than two are set!";
}

?>

Just load the values into an array and pass it to the funtion, it will return the boolean result.

lightnb 05-06-2006 03:47 AM

Thanks.

Does this work:

PHP Code:

//we place all the values into an array so we can run a function on them

$values = @array($HD$VD$TD$Dv$Dh$PC$FC$V$A$W$FA$MFv$MFh$GT1$GT2);


//we check to see if the user has filled in at least two variables

function atLeastTwo($values)
{
    
$counter 0;
    foreach (
$values as $value)
    {
        if (
$value !== null)
        {
            
$counter++;
            if (
$counter == 2)
            {
                return 
false;
            }
        }
    }
    return 
true;
}



if (
atLeastTwo($values))
{
    die 
"You Must fill in at least two fields before calculating";




All times are GMT. The time now is 06:04 AM.

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.01051 seconds
  • Memory Usage 1,751KB
  • 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
  • (6)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete