Open In App

How to find the Largest Number among three Numbers in PHP ?

Last Updated : 03 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given 3 numbers, find the largest number among the three numbers. If the first number is greater than the second and third, print it as the greatest. Otherwise, compare the second and third numbers and print the greater one.

For Instance, Consider the below example where the value of z is greater than the value of x and the value of y.

Input: x = 12, y = 7, z = 15
Output: 15

Using the max( ) Function

The max() function of PHP is used to find the numerically maximum value in an array or the numerically maximum value of several specified values. Here, we will use the max() function for easily find out the largest number among the three numbers.

Example: In this example, we will see how to find largest number in PHP among three numbers by using max function.

PHP




<?php
  
// Input the three numbers
// and store it in variable
  
$number1 = 12;
$number2 = 7;
$number3 = 15;
  
// Using the max function to find the largest number
  
$maxNumber = max($number1, $number2, $number3);
echo "The largest number among three is: $maxNumber\n";
  
?>


Output

The largest number among three is: 15


Using Sorting Method

The sort() function is an inbuilt function in PHP and is used to sort an array in ascending order i.e, smaller to greater. In this method, first all the three numbers are stored in the array after that it is sorted in increasing order by using sort ( ) function of PHP.

Example: In this example, we will see how to find largest number in PHP among three numbers by using sort () function.

PHP




<?php
  
// Input the three numbers
// and store it in variable
  
$number1 = 12;
$number2 = 7;
$number3 = 15;
  
// Using the max function to find the largest number
  
$maxNumber = [$number1, $number2, $number3];
sort($maxNumber);
echo "The largest number among three is: $maxNumber[2]\n";
  
?>


Output

The largest number among three is: 15


Using If-else Method

PHP allows us to perform actions based on some type of conditions, or Decision Making that may be logical or comparative. Based on the result of these conditions i.e., either TRUE or FALSE, an action would be performed as asked by the user. In this method, the if statement is used to execute a block of code based on a specified condition.

Example: This example illsutrates the finding the Largest Number among three Numbers using the If-Else Condition in PHP.

PHP




<?php
  
// Input the three numbers
// and store it in variable
  
$number1 = 12;
$number2 = 7;
$number3 = 15;
  
// Using the max function to find the largest number
  
if ($number1 > $number2 && $number1 > $number3) {
    echo "The largest number is: $number1\n";
} elseif ($number2 > $number1 && $number2 > $number3) {
    echo "The largest number is: $number2\n";
} else {
    echo "The largest number is: $number3\n";
}
  
?>


Output

The largest number is: 15




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads