Home >> PHP >> How can find min difference between two elements from an array in PHP

How can find min difference between two elements from an array in PHP

$a=array(110,20,130,100,2);

$min=$a[0];

$max=$a[0];

$maxdiff=0;

for($i=0;$i<count($a)-1;$i++){

   if($max<$a[$i]){

       $max=$a[$i];

   }

   if($min>$a[$i]){

       $min=$a[$i];

   }

}

$maxdiff=$max-$min;

echo $maxdiff;

Post Your Comment

Next Questions
How can find average from an array
How can find max element from an array
How can find min element from an array
How to connect mysql database with php
Remember password
How can find reverse string without function
How can find reverse word of string without function
How can print reverse string and show in array without function
How to insert data from mysql database
How to update data from mysql database
How to delete data from mysql database
How to list data from mysql database
error_reporting
error_reporting(E_ALL)
What is object oriented programming (oops)
What is class
What is object
What is static variable
What is static method
What is constructor
Why use constructor
Predefined constructor
Parameterized constructor
What is destructor
What is Polymorphism

Copyright ©2022 coderraj.com. All Rights Reserved.