...

View Full Version : How to compare results and echo largest value



jessnoonyes
10-31-2009, 09:04 AM
I have this little script that takes the posted values from a form (yes or no radio buttons) and assigns each answer to a variable. Every time the answer is yes, that variable has a value of +1 added to it. So after the final results are calculated some variables may have a value of 4 and some with a value of 9. Here's a snippet of it:


$one = 0;
if ($_POST['q2'] == 'yes')
$one++;
if ($_POST['q4'] == 'yes')
$one++;

$two = 0;
if ($_POST['q12'] == 'yes')
$two++;
if ($_POST['q13'] == 'yes')
$two++;

echo 'One: ' . $one . '<br />';
echo 'Two: ' . $two . '<br />';

What I'm trying to figure out though is how to check which variable has the highest value, after the points are added up, and echo that one first. Does anyone have an idea of how to do that?

Thank you so much!

karlosio
10-31-2009, 09:50 AM
echo max($one, $two);

Phil Jackson
10-31-2009, 09:51 AM
<?php

$one = 0;
if ($_POST['q2'] == 'yes')
$one++;
if ($_POST['q4'] == 'yes')
$one++;

$two = 0;
if ($_POST['q12'] == 'yes')
$two++;
if ($_POST['q13'] == 'yes')
$two++;

if($one > $two)
{
echo "One has the greater value with ".$one."<br />";
}
elseif($two > $one)
{
echo "Two has the greater value with ".$two."<br />";
}
else
{
echo "Values One and Two have the same value of ".$one."<br />";
}

echo 'One: ' . $one . '<br />';
echo 'Two: ' . $two . '<br />';

?>

jessnoonyes
10-31-2009, 05:05 PM
Wow that's so simple and works great! Thank you!

What do I do if I have more than two variables? Would I write it out kind of like this?


if($one > $two, $three, $four)

abduraooft
10-31-2009, 05:11 PM
No. should be like

if($one > $two && $one > $three && $one > $four)
See http://php.net/manual/en/language.operators.logical.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum