Write a PHP program that accepts three integer values and return true if one of them is 20 or more and less than the subtraction of others.
and Sample Output:
Input the first number : 15
Input the second number: 20
Input the third number : 25
false
Advertisement
Answer
Assuming that you are running the PHP thru a browser. Then the following codes should meet your needs
- check whether one of them is 20 or more and
- check whether it is less than the subtraction of others
<?php
$result = "Please enter data and click CHECK";
if (isset($_POST['submit']))
{
$var1=$_POST["var1"];
$var2=$_POST["var2"];
$var3=$_POST["var3"];
$goodresult = 0;
if ($var1 >= 20 && abs($var2 - $var3) > $var1)
{
$goodresult++;
}
if ($var2 >= 20 && abs($var1 - $var3) > $var2)
{
$goodresult++;
}
if ($var3 >= 20 && abs($var1 - $var2) > $var3)
{
$goodresult++;
}
if ($goodresult != 0)
{
$result = "Test is positive";
}
else
{
$result = "Test is negative";
}
}
?>
<form action="" method=POST>
<table border=0>
<tr><td>First value: <td><input name=var1 value="<?php echo $_POST["var1"]; ?>"><br>
<tr><td>Second value: <td><input name=var2 value="<?php echo $_POST["var2"]; ?>"><br>
<tr><td>Third value: <td><input name=var3 value="<?php echo $_POST["var3"]; ?>"><br>
<tr><td colspan=2><input type=submit name=submit value="Check">
</table>
<?php echo $result; ?>
</form>