Math Functions in PHP: Part 5

Introduction

The PHP math functions handle values within the range of integer and float types. In this article I describe some of the PHP math functions. To learn about some other MATH functions, go to PART 1 , PART 2 and PART 3.

PHP log() function

The PHP math log function is used to return the natural logarithm (base E) of a number.

Syntax

log( number, base)


Parameters in
log function

It takes two parameters; they are:
 

Parameter Description
number It specifies a number.
base The optional logarithmic base to use (defaults to "e" and so to the natural logarithm).

Example of log function

<?php

$val1=log(2);

$val2=log(0);

$val3=log(1.232);

$val4=log(1);

echo "log value of <b>2</b> is <b>$val1</b> <br />";

echo "log value of <b>0</b> is <b>$val2</b> <br />";

echo "log value of <b>1.232</b> is <b>$val3</b> <br />";

echo "log value of <b>1</b> is <b>$val4</b> <br />";

?>

Note: The code above is used to convert numbers to the natural logarithm (base E) of a number. So in the above example the numbers "2", "0", "1.232" and "1" are converted to the natural logarithm of the number.

Output

log-math-function-in-php.jpg
 

PHP log10() function

The PHP math log function is used to return the base-10 logarithm of a number.

Syntax

log10( number)


Parameters in
log10 function

It takes one parameter; it is:
 

Parameter Description
number It specifies a number.

Example of log10 function

<?php

$val1=log10(2);

$val2=log10(0);

$val3=log10(1.232);

$val4=log10(1);

echo "log10 value of <b>2</b> is <b>$val1</b> <br />";

echo "log10 value of <b>0</b> is <b>$val2</b> <br />";

echo "log10 value of <b>1.232</b> is <b>$val3</b> <br />";

echo "log10 value of <b>1</b> is <b>$val4</b> <br />";

?>

Note: The above code is used to convert numbers to the base-10 logarithm of a number. So in the above example the numbers "2", "0", "1.232" and "1" are converted to the base-10 logarithm of the number.

Output

log10-math-function-in-php.jpg

PHP log1p() function

The PHP math log function is used to return the log(1+a) , where a is a number, computed in a way that is accurate even when the value of a is close to zero.

Syntax

log1p( number)


Parameters in
log10 function

It takes one parameter; it is:
 

Parameter Description
number It specifies a number.

Example of log1p function

<?php

$val1=log1p(2);

$val2=log1p(0);

$val3=log1p(1.232);

$val4=log1p(1);

echo "log1p value of <b>2</b> is <b>$val1</b> <br />";

echo "log1p value of <b>0</b> is <b>$val2</b> <br />";

echo "log1p value of <b>1.232</b> is <b>$val3</b> <br />";

echo "log1p value of <b>1</b> is <b>$val4</b> <br />";

?>

Note: The above code is used to convert numbers to the log(1+a). So in the above example the numbers "2", "0", "1.232" and "1" are converted to log(1+a).

Output

log1p-math-function-in-php.jpg


Similar Articles