PHP

Program To Find Largest Number Among Three Numbers In PHP

In this PHP program, we will find the largest number among three numbers provided by the user. The program will take three integer inputs, compare them, and display the largest number.

Explanation

We will ask the user to input three numbers and use conditional statements (if-else) to compare them. The logic for determining the largest number is as follows:

  1. First, we compare if num1 is greater than both num2 and num3. If true, num1 is the largest.
  2. If num1 is not the largest, we check if num2 is greater than num3 and num1. If true, num2 is the largest.
  3. If neither of the above conditions are true, then num3 must be the largest.

PHP Code:

Explanation of the Code:

  1. HTML Form:
    • The user is prompted to enter three numbers in the form fields (num1, num2, and num3).
    • When the user clicks the “Find Largest” button, the form submits the data to the PHP script.
  2. PHP Logic:
    • The isset() function checks if the form is submitted.
    • The values of num1, num2, and num3 are fetched using $_POST.
    • The conditional statements (if-else) compare the values to determine the largest number.
  3. Output:
    • Depending on the condition, the largest number is printed.

Example of Output:




If the user enters the numbers:

  • num1 = 20, num2 = 15, num3 = 25
The output will be:

If the user enters the numbers:

  • num1 = 12, num2 = 25, num3 = 18

The output will be:

Conclusion

This simple PHP program demonstrates how to compare numbers and determine the largest one. By using a basic HTML form and PHP conditional statements, you can easily create such programs to interact with users and display the results based on their inputs.

Leave a Comment