A simple program that I wrote in PHP that will determine the highest and lowest number from the given six numbers given by the user. I don't use any built in function in PHP like max() and min() to check for highest and lowest number. I hope you will find my work useful in your learning PHP programming.
If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Sample Program Output
Program Listing
<?php
error_reporting(0);
$a= $_POST['val_1'];
$b= $_POST['val_2'];
$c= $_POST['val_3'];
$d= $_POST['val_4'];
$e= $_POST['val_5'];
$f= $_POST['val_6'];
if(isset($_POST['clear']))
{
$a=""; $b=""; $c=""; $d=""; $e=""; $f="";
$highest_number=""; $lowest_number="";
}
?>
<html>
<head>
<title>Highest and Lowest Number Checker in PHP</title>
<style>
body { font-family:arial;}
form { width: 400px; }
label { float: left; width: 120px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }
</style>
</head>
<body>
<h2>Highest and Lowest Number Checker in PHP</h2>
<form action="" method="post">
<label>Item Number 1 : </label> <input type="text" name="val_1" value="<?php echo $a; ?>" size="5" maxlength="5" autofocus/><br/><br>
<label>Item Number 2 : </label> <input type="text" name="val_2" value="<?php echo $b; ?>" size="5" maxlength="5"/><br/><br>
<label>Item Number 3 : </label> <input type="text" name="val_3" value="<?php echo $c; ?>" size="5" maxlength="5"/><br/><br>
<label>Item Number 4 : </label> <input type="text" name="val_4" value="<?php echo $d; ?>"size="5" maxlength="5"/><br/><br>
<label>Item Number 5 : </label> <input type="text" name="val_5" value="<?php echo $e; ?>"size="5" maxlength="5"/><br/><br />
<label>Item Number 6 : </label> <input type="text" name="val_6" value="<?php echo $f; ?>"size="5" maxlength="5"/><br/><br />
<input type="submit" value="Ok" name="check" title="Click here to check for the highest and lowest number."/>
<input type="submit" value="Clear" name="clear" title="Click here to clear the text box." />
</form>
<?php
if(isset($_POST['check']))
{
$arr = array($a,$b,$c,$d,$e,$f);
$max =$arr[0];
$min = $arr[0];
foreach($arr as $key => $val){
if($min > $val){
$min = $val;
}
if($max < $val){
$max = $val;
}
}
$highest_number = "The Highest Number is " .$max.".<br>";
$lowest_number = "The Lowest Number is " .$min.".";
echo "<br><br>";
echo "<b>".$highest_number."</b>"."<br>";
echo "<b>".$lowest_number."</b>";
}
?>
</body>
</html>
No comments:
Post a Comment