W3docs

How to Use the expm1() Function in PHP

In this snippet, you will find comprehensive and helpful guidelines on how to use the expm1() function in PHP.

Here, we will have a look at an inbuilt PHP function known as <kbd class="highlighted">expm1()</kbd> that is applied for calculating e raised to the power of a particular argument minus 1. First of all, let’s see the syntax, used for this function:

php expm1 function

float expm1($power)

It is capable of taking a single parameter <kbd class="highlighted">$power</kbd> specifying the power e should be raised to. This function is capable of returning a floating point value that is the value of a raised to the $power of the specific argument -1. So, (e$power-1) will be returned by it.

For a better perception of the function, take a look at the example below:

php demonstrate the expm1() function

<?php

// PHP program to demonstrate the expm1() function
$n1 = 1;
$n2 = 0;
//prints value of e^1 - 1
echo "e^", $n1, "-1 = ", expm1($n1), "\n";
//prints value of e^0 - 1
echo "e^", $n2, "-1 = ", expm1($n2), "\n";

php demonstrate the expm1() function, output

e^1-1 = 1.718281828459
e^0-1 = 0

Now, let’s see another example where the <kbd class="highlighted">expm1()</kbd> function is demonstrated by using an array:

php demonstrate the expm1() function by using an array

<?php

// PHP code to illustrate the working
// of expm1() Function
// input array
$array = [2, 3, 1, 5];
// print all the e^x-1 values in the arrays
foreach ($array as $i) {
  echo 'e^' . $i . '-1 = ' . expm1($i) . "\n";
}

php php demonstrate the expm1() function by using an array, output

e^2-1 = 6.3890560989307
e^3-1 = 19.085536923188
e^1-1 = 1.718281828459
e^5-1 = 147.41315910258

For more information and examples of using the <kbd class="highlighted">expm1()</kbd> function, you can refer to this page.