> A function in programing is method in a program specifically designed to perform a specific operation or task.
> Basically a function has got 3 parts that is, arguments, what to do and what to return.
> However, it is not necessary to supply arguments if not required. To effect the performance of a function, it must be called by just its name followed by brackets and in then supplied with arguments if required.
> Quite often PHP has got in-build math functions which can help you to do tasks quickly without writing your own function.
> Let us an example where we write our own function which calculates the square-root of a number between 0 and 100 and then see how efficient it could have been if we used the PHP sqrt() method.
> Our own function example
$answer = 0;
// declaring that the variable $answer is global ,i.e. can be used by this function
for($m = 0 ; $m <= 100 ; $m++)
if($m * $m == $number)
// if a number is multiplied by itself and gives the result as the number we are to find its square root
$answer = $m;
my_sqrt(64); // this is how we call a function
You will get Output on browser as shown below;
> However, if we could have used the in-build PHP sqrt() function it could have reduced out work. i.e.consider the same example but without using our own example,here we are using PHP sqrt() function.
> PHP sqrt() function example.
echo sqrt(64); // this is how we call a function
You will get same Output on browser as shown below;
> Some of the in-build PHP math functions are explained below;
> This function is used to get the absolute value of the number.