View Full Version : Syntax: checking if multiple variables are set
lightnb
05-06-2006, 02:42 AM
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:
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 ;
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?
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
$A = 3;
$B = 4;
if (isset($A) and isset($B) and !isset($C))
{
$C = sqrt(pow($A, 2) + pow($B, 2));
}
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
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:
//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";
}
Adrian Schneider
05-06-2006, 04:28 AM
if (!atLeastTwo($values))
{
die('You Must fill in at least two fields before calculating'):
}
Note the ! (right now it will stop when there at least two values, when you want the opposite instead).
Just to clarify this comment "//we check to see if the user has filled in at least two variables" - nothing actually happens here yet. It is just the function declaration, but it does check when you actually use the function below.
Other than that, yes - it should work.
lightnb
05-06-2006, 05:08 AM
I swapped the true and false in the function though, so if theres more than two the function is false. so if there's less than two, the function will be true, and cause the script to die. (I think)
I'm trying to run the script now, but I'm getting a parse error:
Parse error: parse error, unexpected $ in /home/.sites/22/site13/web/livexchange/tools/photocalc.php on line 295
but there is no dollar sign!
I've even tried deleting the last lines of the script, but it still says the last line has a dollar sign.
This is the whole thing, with a bunch of sections commented out:
<?PHP
////////////////////////////////////////////////////
//define function user form
function user_form() {
// now to output the form HTML.
echo '<p>Fill in some fields.</p>';
echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">';
echo '<table border="0" cellspacing="4" cellpadding="0">';
echo '<tr><td>Throw Distance:</td><td><input type="text" name="TD" value="'.htmlspecialchars($_POST['TD']).'"></td></tr>';
echo '<tr><td>Vertical Distance:</td><td><input type="text" name="VD" value="'.htmlspecialchars($_POST['VD']).'"></td></tr>';
echo '<tr><td>Horizontal Distance:</td><td><input type="text" name="HD" value="'.htmlspecialchars($_POST['HD']).'"></td></tr>';
echo '<tr><td colspan="2"><input type="submit" value="Calculate" name="submit"></td></tr>';
echo '</table>';
echo '</form>';
}
//end function user form
//////////////////////////////////////////////////////
if (isset($_POST['submit'])) {
// the form has been submitted
// perform data checks.
$error_str = ''; // initialise $error_str as empty
$HD;
$VD;
$TD;
$Dv;
$Dh;
$PC;
$FC;
$V;
$A;
$W;
$FA;
$MFv;
$MFh;
$GT1;
$GT2;
//begin executing the script
//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";}
//we check if all our variables are numbers
if( isset($TD) ) { if(!is_numeric($TD)){$error_str .= 'td must be a number'; }
if( isset($Dv) ) { if( !is_numeric($Dv)){$error_str .= 'td must be a number'; }
if( isset($Dh) ) { if( !is_numeric($Dh)){$error_str .= 'td must be a number'; }
if( isset($MFh) ) { if( !is_numeric($MFh)){$error_str .= 'td must be a number'; }
if( isset($MFv) ) { if( !is_numeric($MFv)){$error_str .= 'td must be a number'; }
if( isset($FA) ) { if( !is_numeric($FA)){$error_str .= 'td must be a number'; }
if( isset($PC) ) { if( !is_numeric($PC)){$error_str .= 'td must be a number'; }
if( isset($HD) ) { if( !is_numeric($HD)){$error_str .= 'td must be a number'; }
if( isset($VD) ) { if( !is_numeric($VD)){$error_str .= 'td must be a number'; }
if( isset($GT1) ) { if( !is_numeric($GT1)){$error_str .= 'td must be a number'; }
if( isset($GT2) ) { if( !is_numeric($GT2)){$error_str .= 'td must be a number'; }
//convert the gel transmission rates from percents to decimals, if they exist
//if $GT1 {we convert the percent entered by the user to a decimal and save it to $GT1};
//if $GT2 {we convert the percent entered by the user to a decimal and save it to $GT2};
if (!empty($error_str)) {
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>' . $error_str . '</ul>';
// show form again
user_form();
exit; // die
}
///////////////////// Syntax Corrected Below! ///////////////////////////
// check if we have to many variables and deal with it
//if ( isset($HD) and isset($VD) and isset($TD) ) {if $TD !== sqrt(($VD*$VD)+($HD*$HD)) {die "We're sorry, but you can't change the laws of physics!";}}
// check if we have HD and VD and if so, solve for TD
//if ( isset($HD) and isset($VD) and !isset($TD) ) {$TD=sqrt(($VD*$VD)+($HD*$HD));}
//check for and solve possible variances of d = mf * td for the verticle axis
//if ( isset($Dv) and isset($MFv) and isset($TD) ) {if $Dv !== ($MFv*$TD) {die 'physics cannot be altered!!!'};
//}else if ( isset ($TD) and isset($MFv) and !isset($Dv) ) {$Dv = ($MFv*$TD);
//}else if ( isset ($TD) and isset($Dv) and !isset($MFv) ) {$MFv = ($Dv/$TD};
//}else if ( isset ($MFv) and isset($Dv) and !isset($TD) ){$TD = ($Dv/$MFv)};
//}
//check for and solve possible variances of d = mf * td for the horizontal axis
//if ( isset($Dh) and isset($MFh) and isset($TD) ) {if $Dh !== ($MFh*$TD) {die 'physics cannot be altered!!!'};
//}else if ( isset ($TD) and isset($MFh) and !isset($Dh) ) {$Dv = ($MFh*$TD);
//}else if ( isset ($TD) and isset($Dh) and !isset($MFh) ) {$MFh = ($Dh/$TD};
//}else if ( isset ($MFh) and isset($Dh) and !isset($TD) ){$TD = ($Dh/$MFv)};
//}
////////////////////// End Corrected Syntax ///////////////////////////
//we solve for variances of mf=2 x TAN (FA/2)
//if ( isset($MF) and !isset($FA) ) {$FA = };
//if ( !isset($MF) and isset($FA) ) {$MF = ('2'*(};
//if we have MF now and didn't before, we go back to the top of the script (where is says 'begine executing the script'), and run it again now that we have a new variable set
//if we have MF now and didnt before {go back to top and run again with the new variable};
// if we can, we consider the gel transmission before going on
//if $FC isset and $PC !isset {
// if $FC and $GT1 and not $GT2 isset {solve for $PC using FC/$GT1=$PC and save the answer as $PC
//}else if{ $FC and $GT2 and not $GT1 isset {solve for $PC using FC/$GT2=$PC and save the answer as $PC
// }else if{ $FC and $GT1 and $GT2 isset {solve for $PC using FC/$GT2/$GT1=$PC and save the answer as $PC};
//if $FC and $GT1 isset {multiple $FC by $GT1 and save the answer as $FC}
//if $FC and $GT2 isset {multiple $FC by $GT2 and save the answer as $FC}
//we solve for variances of fc= pc /(tdv squared)
//if we have PC and TD and not FC {we solve for PC, and set $PC = to the answer and if $GT1 isset {we multiply the new $FC by $GT1 and store the answer as $FC} and if $GT2 isset, we multiple $FC by $GT2 as well, and agin, store the answer as $FC
//}else if we have FC and TD and not PC {we solve for PC, and set $FC = to the answer
//}else if we have PC and FC and not TD {we solve for TD and set $TD = the answer
//}else if we dont have PC and dont have FC and dont had TD {we ignore this set, and carry on
//}else if we have TD and FC and PC {if FC and PC and TD calculate out corectly {carry on}else{die 'physics cannot be changed'};
//if we have TD now and didn't before, we go back to the top of the script (where is says 'begine executing the script'), and run it again now that we have a new variable set
//if we have TD now and didnt before {go back to top and run again with the new variable};
//Now that we have out answers, lets spit them out, and go to bed, since it's 4 AM
//if $MFv isset {spit it out into the approprate field on the form];
//if $MFh isset {spit it out into the approprate field on the form];
//if $GT1 isset {spit it out into the approprate field on the form];
//if $GT2 isset {spit it out into the approprate field on the form];
//if $HD isset {spit it out into the approprate field on the form];
//if $VD isset {spit it out into the approprate field on the form];
//if $TD isset {spit it out into the approprate field on the form];
//if $Dv isset {spit it out into the approprate field on the form];
//if $Dh isset {spit it out into the approprate field on the form];
//if $PC isset {spit it out into the approprate field on the form];
//if $FC isset {spit it out into the approprate field on the form];
//if $FA isset {spit it out into the approprate field on the form];
//if $V isset {spit it out into the approprate field on the form];
//if $A isset {spit it out into the approprate field on the form];
//if $W isset {spit it out into the approprate field on the form];
if (!empty($error_str)) {
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>' . $error_str . '</ul>';
// show form again
user_form();
exit; // die
}
// if we get here, all data checks were okay, process information as you wish.
} else {
// the form has not been submitted, let's show it
user_form();
?>
lightnb
05-09-2006, 01:47 AM
Here's a simpler version, that's still giving me the unexpected $ on the last line error:
<?php
// function to output form and hold previously entered values.
function user_form() {
//output the form HTML.
echo '<p>Please fill in at least two fields.</p>';
echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">';
echo '<table border="0" cellspacing="4" cellpadding="0">';
echo '<tr><td>Vertical Distance:</td><td><input type="text" name="VD" value="'.htmlspecialchars($_POST['VD']).'"></td></tr>';
echo '<tr><td>Horizontal Distance:</td><td><input type="text" name="HD" value="'.htmlspecialchars($_POST['HD']).'"></td></tr>';
echo '<tr><td>Throw Distance:</td><td><input type="text" name="TD" value="'.htmlspecialchars($_POST['TD']).'"></td></tr>';
echo '<tr><td colspan="2"><input type="submit" value="Add to Database" name="submit"></td></tr>';
echo '</table>';
echo '</form>';
}
// has the form been submitted?
if (isset($_POST['submit'])) {
// the form has been submitted
// perform data checks.
$error_str = ''; // initialise $error_str as empty
if( isset($_POST['VD'])) { if(!is_numeric($_POST['VD'])){$error_str .= '<li>VD must be a number</li>'; }
if( isset($_POST['HD']) ) { if( !is_numeric($_POST['HD'])){$error_str .= '<li>HD must be a number</li>'; }
if( isset($_POST['TD']) ) { if( !is_numeric($_POST['TD'])){$error_str .= '<li>TD must be a number</li>'; }
// now, have any of these errors happened? We can find out by checking if $error_str is empty
if (!empty($error_str)) {
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>'.$error_str.'</ul>';
// show form again
user_form();
exit; // die
}else{echo'Success!!';}
// if we get here, all data checks were okay, process information as you wish.
} else {
// the form has not been submitted, let's show it
user_form();
}
?>
Adrian Schneider
05-09-2006, 05:43 AM
I find it much easier when you indent everything "properly" (to my eyes, anyway). After doing so, I found the problem fairly quickly:<?php
// function to output form and hold previously entered values.
function user_form()
{
//output the form HTML.
echo '<p>Please fill in at least two fields.</p>';
echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">';
echo '<table border="0" cellspacing="4" cellpadding="0">';
echo '<tr><td>Vertical Distance:</td><td><input type="text" name="VD" value="'.htmlspecialchars($_POST['VD']).'"></td></tr>';
echo '<tr><td>Horizontal Distance:</td><td><input type="text" name="HD" value="'.htmlspecialchars($_POST['HD']).'"></td></tr>';
echo '<tr><td>Throw Distance:</td><td><input type="text" name="TD" value="'.htmlspecialchars($_POST['TD']).'"></td></tr>';
echo '<tr><td colspan="2"><input type="submit" value="Add to Database" name="submit"></td></tr>';
echo '</table>';
echo '</form>';
}
// has the form been submitted?
if (isset($_POST['submit']))
{
// the form has been submitted
// perform data checks.
$error_str = ''; // initialise $error_str as empty
if (isset($_POST['VD']))
{
if (!is_numeric($_POST['VD']))
{
$error_str .= '<li>VD must be a number</li>';
}
if (isset($_POST['HD']))
{
if (!is_numeric($_POST['HD']))
{
$error_str .= '<li>HD must be a number</li>';
}
if (isset($_POST['TD']))
{
if (!is_numeric($_POST['TD']))
{
$error_str .= '<li>TD must be a number</li>';
}
// now, have any of these errors happened? We can find out by checking if $error_str is empty
if (!empty($error_str)) {
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>'.$error_str.'</ul>';
// show form again
user_form();
exit; // die
}else{echo'Success!!';}
// if we get here, all data checks were okay, process information as you wish.
} else {
// the form has not been submitted, let's show it
user_form();
}
?>I stopped midway as you can see, but I found two errors already (forgot to close off braces). I'll finish it now the way I think you were going for... The other change I made is combining some of your ifs -
if ($a)
{
if ($b)
{
// do stuff
{
}
// is the same as :
if ($a and $b)
{
// do stuff
}Working copy (as far as executing without errors)
<?php
// function to output form and hold previously entered values.
function user_form()
{
//output the form HTML.
echo '<p>Please fill in at least two fields.</p>';
echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">';
echo '<table border="0" cellspacing="4" cellpadding="0">';
echo '<tr><td>Vertical Distance:</td><td><input type="text" name="VD" value="'.htmlspecialchars($_POST['VD']).'"></td></tr>';
echo '<tr><td>Horizontal Distance:</td><td><input type="text" name="HD" value="'.htmlspecialchars($_POST['HD']).'"></td></tr>';
echo '<tr><td>Throw Distance:</td><td><input type="text" name="TD" value="'.htmlspecialchars($_POST['TD']).'"></td></tr>';
echo '<tr><td colspan="2"><input type="submit" value="Add to Database" name="submit"></td></tr>';
echo '</table>';
echo '</form>';
}
// has the form been submitted?
if (isset($_POST['submit']))
{
// the form has been submitted
// perform data checks.
$error_str = ''; // initialise $error_str as empty
if (isset($_POST['VD']) and !is_numeric($_POST['VD']))
{
$error_str .= '<li>VD must be a number</li>';
}
if (isset($_POST['HD']) and !is_numeric($_POST['HD']))
{
$error_str .= '<li>HD must be a number</li>';
}
if (isset($_POST['TD']) and !is_numeric($_POST['TD']))
{
$error_str .= '<li>TD must be a number</li>';
}
// now, have any of these errors happened? We can find out by checking if $error_str is empty
if (!empty($error_str))
{
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>'.$error_str.'</ul>';
// show form again
user_form();
exit; // die
}
else
{
echo 'Success!!';
}
// if we get here, all data checks were okay, process information as you wish.
}
else
{
// the form has not been submitted, let's show it
user_form();
}
?>Cheers
lightnb
05-09-2006, 07:04 AM
Thank you! I didn't know there was a certain way to indent stuff (this is my first script), but now that I see it, it makes sense. :)
Is there a way to make Dreamweaver do the indenting for you?
I've added a bit more code to it, and I've got the new stuff running without errors, but It's not behaving as expected.
If I leave the "TD" input field blank, the code:
if (isset($_POST['TD']) and !is_numeric($_POST['TD']))
Should evaluate false, and not generate a user error message. But it does...
If I give it all three values, this should check if they all add up corectly:
if ( isset($HD) and isset($VD) and isset($TD) )
{if ( $TD !== sqrt(($VD*$VD)+($HD*$HD)) )
{$error_str .= "<li>We're sorry, but you can't change the laws of physics!</li>";
}
}
But it says they're wrong even when I use 3,4, and 5, which should evaluate corectly.
And finally, this:
if ( isset($HD) and isset($VD) and !isset($TD) )
{$TD=sqrt(($VD*$VD)+($HD*$HD));
echo $TD;
}
should take the first two input numbers, do the math, and spit out the answer (preferably back in the third input field), but it only gives me a blank page (when I comment out the other sections, so that I can get that far).
Here's the whole thing in context:
<?php
// function to output form and hold previously entered values.
function user_form()
{
//output the form HTML.
echo '<p>Please fill in at least two fields.</p>';
echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">';
echo '<table border="0" cellspacing="4" cellpadding="0">';
echo '<tr><td>Vertical Distance:</td><td><input type="text" name="VD" value="'.htmlspecialchars($_POST['VD']).'"></td></tr>';
echo '<tr><td>Horizontal Distance:</td><td><input type="text" name="HD" value="'.htmlspecialchars($_POST['HD']).'"></td></tr>';
echo '<tr><td>Throw Distance:</td><td><input type="text" name="TD" value="'.htmlspecialchars($_POST['TD']).'"></td></tr>';
echo '<tr><td colspan="2"><input type="submit" value="Add to Database" name="submit"></td></tr>';
echo '</table>';
echo '</form>';
}
// has the form been submitted?
if (isset($_POST['submit']))
{
// the form has been submitted
// perform data checks.
$error_str = ''; // initialise $error_str as empty
if (isset($_POST['VD']) and !is_numeric($_POST['VD']))
{
$error_str .= '<li>VD must be a number</li>';
}
if (isset($_POST['HD']) and !is_numeric($_POST['HD']))
{
$error_str .= '<li>HD must be a number</li>';
}
if (isset($_POST['TD']) and !is_numeric($_POST['TD']))
{
$error_str .= '<li>TD must be a number</li>';
}
// now, have any of these errors happened? We can find out by checking if $error_str is empty
if (!empty($error_str))
{
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>'.$error_str.'</ul>';
// show form again
user_form();
exit; // die
}
else
//Here is where we put all the stuff we want it to do!
{
$error_str = ''; //Reset the error store
if ( isset($HD) and isset($VD) and isset($TD) )
{if ( $TD !== sqrt(($VD*$VD)+($HD*$HD)) )
{$error_str .= "<li>We're sorry, but you can't change the laws of physics!</li>";
}
}
if ( isset($HD) and isset($VD) and !isset($TD) )
{$TD=sqrt(($VD*$VD)+($HD*$HD));
echo $TD;
}
if (!empty($error_str))
{
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>'.$error_str.'</ul>';
// show form again
user_form();
exit; // die
}
}
// if we get here, all data checks were okay, process information as you wish.
}
else
{
// the form has not been submitted, let's show it
user_form();
}
?>
I tried to do the indenting thing with all the brackets.
This is my first time doing this, and I appreciate your taking the time to help me. :)
Adrian Schneider
05-09-2006, 05:39 PM
You should do indenting manually... your editor should automatically indent to where you were on the previous line when you press enter, if not look at the settings. Whenever you open braces, the line after it should be indented further, and when you close that set of braces off go back to the left one so they line up. It makes it much easier to read it without going into detail!
Isset() checks if the variable is SET. So this means if <input type="text" name="TD" /> (for example) is in your form, regardless of whether or not you enter any data into it), it will be set. I would suggest one of the following intead:
// replace $var with what you are checkin
if (!$var)
{
// checks if $var is false (this will work with an empty string)
}
if ($var == '' OR $var === '')
{
/*
Checks if $var is is an empty string. Second method is probably preferred
because it checks type as well.
*/
}
if (empty($var))
{
/*
Checks if $var is empty:
A variable is empty if it is any of the following:
"" (empty string)
0 (integer)
"0" (string)
NULL (or not set yet)
FALSE
array() (empty array)
It also checks if the variable isset.
*/
}
lightnb
05-14-2006, 04:28 PM
Thank you.
I added a section to unset all post variables with no data:
if ($_POST['VD'] === '')
{unset($VD);
}
else
{$_POST['VD'] = $VD;
}
if ($_POST['HD'] === '')
{unset($HD);
}
else
{$_POST['HD'] = $HD;
}
if ($_POST['TD'] === '')
{unset($TD);
}
else
{$_POST['TD'] = $TD;
}
It's calculating everything correctly now, and it's not giving errors, but it's spitting the answer out on a blank page.
How can I take my answer ($TD) and set it equall to .htmlspecialchars($_POST['TD']). so that it displays the answer in the unfilled form field?
can I just use:
.htmlspecialchars($_POST['TD']). = $TD
Thanks,
Nick
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.