Page 1 of 1

How to optimize multiple (!$vars) ...?

Posted: Tue Dec 10, 2002 12:49 pm
by rxsid
Hi all,
How could I optimize this server side check for missing form data?

Code: Select all

if(!$varA || !$varB || !$varC || !$varD || !$varF || !$varG) {
   //some error for missing form data
}
Can the var's to check be put in some kind of array?

thanks...

...

Posted: Tue Dec 10, 2002 1:24 pm
by QWERTY
Create an array with names of variables, than do foreach() and create a string for matching in IF sentense ( $var1 || $var2, etc ... ). Then use eval() to create IF() ...

Code:

Code: Select all

<?php
$arrayVariables = array( "varA", "varB", "varC", "varD" );

    $intX = 0;
    $intNumOfValues = count( $arrayVariables );
foreach( $arrayVariables AS $strVariable )&#123;
    $intX++;
        if( $intX == $intNumOfValues )&#123;
            $strToEval .= "\$" . $strVariable;
        &#125;else&#123;
            $strToEval .= "\$" . $strVariable  . " || ";
        &#125;
&#125;

eval( "if( " . $strToEval . " )&#123; echo "Yeah";&#125;else&#123; echo "Nooo";&#125;" );

?>
Something like that ...