For people interest in Differential Equations, I've done a function that receive a string like: x^2+x^3 and put it in2x+3x^2 witch is the differantial of the previous equation.

In the code there is one thing missing: the $string{$i} is often going outOfBound (Uninitialized string offset: 6 in...)if your error setting is set a little too high... I just dont know how to fix this.

I needed to approximate an integral because i was not able to calculate it, so i wrote this function. It approximates an integral with the composite Simpson's rule.More information on Simpson's rule: http://en.wikipedia.org/wiki/Simpson%27s_rule

while joogat's one line function is short, it is probably better to calculate factorial iteratively instead of recursively. keep in mind if you want large factorials, you'll need to use some sort of arbitrary precision integer or perhaps the BCMath functions. then again, unless you're trying to do large numbers (170! is the highest that you can do that does not return infinity) you probably won't notice any time difference.<?phpfunction factorial($in) {// 0! = 1! = 1$out = 1;

Two functions I didn't find elsewhere... one to compute mean of an array of numbers, and another to computer variance of a sample of numbers. Both take an array of numbers as arguments. Not much error checking, or optimization...

If you need to deal with polar co-ordinates for somereason you will need to convert to and from x,y for input and output in most situations: here are some functions to convert cartesian to polar and polar to cartesian<?//returns array of r, theta in the range of 0-2*pi (in radians)function rect2polar($x,$y){ if(is_numeric($x)&&is_numeric($y)) { $r=sqrt(pow($x,2)+pow($y,2)); if($x==0) { if($y>0) $theta=pi()/2; else $theta=3*pi()/2; } else if($x<0) $theta=atan($y/$x)+pi(); else if($y<0) $theta=atan($y/$x)+2*pi(); else $theta=atan($y/$x); $polar=array("r"=>$r,"theta"=>$theta); return $polar; } else return false;}

Here's yet another greatest common denominator (gcd) function, a reeeeally small one.

function gcd($n,$m){if(!$m)return$n;return gcd($m,$n%$m);}

It works by recursion. Not really sure about it's speed, but it's really small! This won't work on floating point numbers accurately though. If you want a floating point one, you need to have at least PHP 4, and the code would be

Please note that shorter is not always better
(meaning that really short faculty implementation above).

In my opinion, a clearer way to code this is, including a check
for negative or non-integer values.

In order to calculate the faculty of a positive integer,
an iterative way (which might be harder to understand)
is usually a bit faster, but I am using it only for small
values so it is not really important to me:

I've also written another function to calculate the
binomial coefficient of 2 values, I didn't find it anywhere yet so I hope it might help someone (works fine with the above stated faculty-function and ready to be used inside of your own classes!)

Occasionally a user must enter a number in a form. This function converts fractions to decimals and leaves decimals untouched. Of course, you may wish to round the final output, but that is not included here.

The reason the bitwise AND ("&") operator works to determine whether a number is odd or even is because odd numbers expressed in binary always have the rightmost (2^0) bit = 1 and even numbers always have the 2^0 bit = 0.

So if you do a " 1 & $num", it will return zero if the number is even (since xxxxxxx0 [the even number in binary] and 00000001 [the 1]) don't share any bits, and will return 1 if the number is odd (xxxxxx1 and 000001).

a clever way of doing things, but $num % 2 would work as well i think :).

Tim's fix of Evan's ordinal function causes another problem, it no longer works for number above 100. (E.g. it returns 111st instead of 111th). Here is a further modified version which should work for all numbers.

This is my factorial method<?phpfunction factorial($nr){$product = array_product(range(1,++$nr)); return $product / $nr;}?>It uses the reversed method by applying division instead of multiplication, so it even returns the right answer when entering 0.

or similar. At least on PHP 4.3.10, substr("1", -2, 1) returns '1' - so Evan's function gives "1th", as well as "11th". This is contrary to the documentation, but is noted in the comments on the substr manual page.

Method to convert an arbitrary decimal number to its most reduced fraction form (so a string is returned, this method would probably be used for output formatting purposes.) There were other methods similar to this one on the page, but none did quite what I wanted. It's maybe not the most elegant code, but it gets the job done. Hope this helps someone. An iterative form of Euclid's algorithm is used to find the GCD.

<?php function lcd($n,$m, $maxvarianzpercent=0){// set $maxvarianzpercent=5 to get a small, but approx. result /* a better lcd function with varianz: for example use lcd(141,180,5) to get the approx. lcd '7/9' which is in fact 140/180 */ // ATTENTION!!! can be really slow if $m is >1000