How to use the max( ) Function In PHP

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


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

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

Table of Content

  • Using the max Function
  • Using Sorting Method
  • Using If-else Method

Similar Reads

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....

Using Sorting Method

...

Using If-else 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....

Contact Us