Maximum sum sub-array

// Using Kadane's algorithm

$array = [1, -3, 2, -5, 7, 6, -1, -4, 11, -23];

var_dump(findSubarraySum($array));

function findSubarraySum($array) {
   $start = 0;
   $end = 0;
   $sum = 0; 

   $tempStart = 0;
   $tempEnd = 0;
   $tempSum = 0;
   $currPos = 0;

   foreach ($array as $item) {

      if ($tempSum + $item <= 0) {
         $tempSum = 0;
         $tempStart = $currPos + 1;
      } else {
         $tempSum += $item;
         $tempEnd = $currPos;
      }
 
      // change indices based on temp run
      if ($tempSum > $sum) {
         $sum = $tempSum;
         $start = $tempStart;
         $end = $tempEnd;
      }

      $currPos++;
   }

   print "$sum, $start, $end";
}
    
index