PHP code to calculate gamma distributions and cumul gamma distributions – similar to excel’s gammadist() functions.

function getgamma($x, $k = 1, $theta = 1) {
		return pow($x, $k - 1)*exp(-$x/$theta)/(gamma($k)*pow($theta, $k));
	}

function getcumulgamma($x, $k = 1, $theta = 1) {
		return lowerGamma($k, $x/$theta)/gamma($k);
	}

function lowerGamma($s, $x) {
		//Special thanks to http://www.reddit.com/user/harlows_monkeys for this algorithm.
		if ($x == 0) return 0;
		$t = exp($s*log($x)) / $s;
		$v = $t;
		for ($k = 1; $k < 150; ++$k) {
			$t = -$t * $x * ($s + $k - 1) / (($s + $k) * $k);
			$v += $t;
			if (abs($t) < 0.00000000001) break;
		}
		return $v;
	}

function gamma($x) {
		if ($x <= 0) {
		return NAN;
		} else {
			return sqrt(2*M_PI/$x)*pow((1/M_E)*($x+(1/(12*$x - 1/(10*$x)))), $x);
		}
	}