Php program to find largest number in an array

PHP Program To Find The Largest Number of Given Values

In this tutorial, you gonna learn find the largest of given numbers in PHP.
In PHP, We have a function max() by using this function we can easily get the largest number.

But I will first go for PHP code to find the largest of 3 numbers. By this, we can get the logic first.
Thereafter, We gonna learn the easiest way to find out the largest number among some given numbers. No matter how many numbers are provided to find out the largest numbers among those.

PHP Program to find out the largest of three numbers

$num2 && $num1>$num3){
      	echo $num1;
      }
      else{
      	if($num2>$num1 && $num2>$num3){
      		echo $num2;
      	}
      	else
      		echo $num3;
      }

?>

This will echo out the largest number of those three numbers.

But now assume that you have more than three numbers or more than that what will you do?

For this, The simple concept is to put all the numbers in an Array and echo out the largest number using the max() function.

let’s see an example

PHP program to echo out the largest number of given numbers

In PHP this function gonna return the maximum value in an Array.

So you may use the below Code too

It will also give you the same output.

Also, Read

Check if a string contains a particular word using PHP

How To Generate Random Password In PHP?

PHP program to find the largest of three numbers using HTML form as input.




  Largest Number


Enter the first number:

Enter the second number:

Enter the third number:

$num2 && $num1>$num3){ echo $num1; } else{ if($num2>$num1 && $num2>$num3){ echo $num2; } else echo $num3; } } ?>

The above is an example of finding the largest of three numbers where a user can input the numbers via HTML form.

Remove duplicate values from an array in PHP

Write a PHP code to find second largest number in array. Given an unsorted array, we have to write a PHP program to find the second largest number in an array.

Apart from solving this problem. We have to focus on time complexity. As the time complexity of an algorithm is very important in terms of how efficient your algorithm is.

For example – Let’s take an array.

Input – arr[] = { 4, 9, 5, 2, 8, 0, 3, 22}

Output – 9

The second largest element in this array is 9.

We have discussed the problem statement. Let’s think, how we can solve this problem efficiently? There are multiple ways to solve this problem. Which approach you prefer and why?

How to Find Second Largest Number in Array – PHP Code

Approach 1:

One approach is to sort an array. PHP provides several functions for sorting an array. After sorting, pick the element present at n-2 position where n is the size of an array.

The time complexity of this approach is O(nlogn).

functionsecondHighest(array$arr){

    sort($arr);

    echo $arr[sizeof($arr)-2];

}

secondHighest(array(4,9,5,2,8, 0,3,22));

NOTE: If the element of an array is repeated then this approach won’t work.

To understand this concept, let’s take an example.

int num[] = {1, 9, 5, 55, 8, -1, 3, 55};

So what will be the second highest number if we use above method (sort and pick the element present at n-2 index). The element is 55 which is wrong.

Approach 2:

Traverse an array and maintain two indexes max and second max. The code for this approach is written below.

PHP Code to Find Second Largest Number in an Array

The idea here is to find the second largest number using single loop. To do that, declare two variables max and secondMax. Initially, Assign them with Integer minimum possible value (In PHP, we can do that by using PHP_INT_MIN).

In each iteration, Compare the value present at current index with max and secondMax variable. If current value is greater than the max then assign max value in secondMax and current value in max variable.

The time complexity of this approach is O(n).

How to Sort String in PHP.

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

39

40

41

42

/**

*

* @param array $arr

* @return second highest number of an array

*/

functionfindSecondMax(array$arr){

    //If array is empty then return

    if(empty($arr)) {

        return;

    }

    /*

     * Initialize max and second max with negative value

     */

    $max= PHP_INT_MIN;

    $secondMax=PHP_INT_MIN;

    //Traverse an array

    foreach($arr as$number){

        //If it's greater than the value of max

        if($number>$max){

            $secondMax=$max;

            $max=$number;

        }

        //If array number is greater than secondMax and less than max

        if($number >$secondMax&&$number<$max){

            $secondMax=$number;

        }

    }

    return$secondMax;

}

$arr=array(70,4,8,10, 14,9,7,6,5,3,2);

$second_maximum=findSecondMax($arr);

echo "Second Highest Element is ".$second_maximum;

Programming video tutorials

How do you find the largest number in an array 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. The max() function can take an array or several numbers as an argument and return the numerically maximum value among the passed parameters.

How do you find the largest number in an array array?

To find the largest element, the first two elements of array are checked and the largest of these two elements are placed in arr[0] the first and third elements are checked and largest of these two elements is placed in arr[0] . this process continues until the first and last elements are checked.

How do I find the smallest and largest number in an array in php?

Approach 2 (Using Library Functions) : We use library functions to find minimum and maximum..
Max():max() returns the parameter value considered “highest” according to standard comparisons. ... .
Min():min() returns the parameter value considered “lowest” according to standard comparisons..

How do you find the highest and second highest number in an array in php?

Find Largest and Second Largest Number in array php $array = array(5,7,81,0,12); $max1 =0 ; $max2 = 0; for($i=0; $i $max1) { $max2 = $max1; $max1 = $array[$i]; } else if($array[$i] > $max2) { $max2 = $array[$i]; } } echo "Maximum value = ".