In this post, we are going to see how to write a simple PHP program to Find the Largest of Three numbers. For this, we need to collect three numbers from the users and then compare all the three numbers to find the Largest. Let’s begin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>PHP Program to Find Largest of Three Numbers</title> </head> <body> <h3>PHP Program to Find Largest of Three Numbers</h3> <form action="" method="post"> Number 1 : <input type="number" name="first" min="0" value="<?=(isset($_POST['first']))?$_POST['first']: 25;?>" /><br /><br /> Number 2: <input type="number" name="second" min="0" value="<?=(isset($_POST['second']))?$_POST['second']: 75;?>" /><br /><br /> Number 3: <input type="number" name="third" min="0" value="<?=(isset($_POST['third']))?$_POST['third']: 53;?>" /><br /><br /> <input type="submit" /><br /><br /> </form> </body> </html> <?php //after hit the enter button //form method="post" so we have to use $_POST if($_POST){ //get the value from input field $first = $_POST['first']; $second = $_POST['second']; $third = $_POST['third']; //find the largest number between first and second and store it in a $temp variable $temp = ($first > $second) ? $first : $second; //Now compare the $temp & $third variable to find the largest number of 3 $largest = ($third > $temp) ? $third : $temp; print "<h2>Largest of three number is: ".$largest."</h2>"; } ?> |
I have used the ternary operator to find the Largest value. Go through the above program and read the comments to understand it. Create a new PHP file and copy-paste the above script to execute it.
You can also check the demo here: