PHP exp() function


The exp() function in PHP calculates the value of e raised to the power of a given number. Here, e is Euler's number, which is approximately equal to 2.71828. This function is commonly used in mathematics, particularly in calculations involving exponential growth or decay, compound interest, and in solving differential equations.

Syntax:

exp(float $number): float
  • $number: The exponent to which e is raised.
  • Return Value: Returns the value of e raised to the power of the given number as a float.

Example 1: Basic Usage

<?php echo exp(1); ?>

Output:

2.718281828459

Explanation: This calculates e raised to the power of 1, which equals e.

Example 2: Exponent of Zero

<?php echo exp(0); ?>

Output:

1

Explanation: Any number raised to the power of 0 is 1, so e^0 = 1.

Example 3: Negative Exponent

<?php echo exp(-1); ?>

Output:

0.36787944117144

Explanation: This calculates e raised to the power of -1, which is approximately 0.3679. This is the same as 1e\frac{1}{e}.

Example 4: Larger Exponents

<?php echo exp(3); ?>

Output:

20.085536923188

Explanation: Here, e is raised to the power of 3, resulting in approximately 20.0855.

Example 5: Using with Other Mathematical Functions

<?php $base = 5; $exponent = 3; // Calculate e^(base * exponent) $result = exp($base * $exponent); echo $result; ?>

Output:

148403.755066

Explanation: In this case, it calculates e raised to the power of 5×3=155 \times 3 = 15.

Key Points:

  • Use Cases: The exp() function is often used in fields such as finance for calculating compound interest, in physics for exponential decay models, and in various statistical applications.
  • Precision: The output is a floating-point number, so it maintains a reasonable level of precision for most practical applications.
  • Mathematical Context: Exponential functions are fundamental in calculus and are often used to model real-world scenarios, such as population growth, radioactive decay, and more.

Example of Practical Use in Finance:

<?php $principal = 1000; // Initial investment $rate = 0.05; // 5% interest rate $time = 10; // 10 years // Calculate the amount after 10 years using the formula A = P * e^(rt) $amount = $principal * exp($rate * $time); echo "Amount after 10 years: " . $amount; ?>

Output:

Amount after 10 years: 1648.7212707001

Explanation: This example uses exp() to calculate the future value of an investment using continuous compounding.

In summary, the exp() function in PHP is a powerful mathematical tool for calculating exponential values, with numerous applications in science, engineering, finance, and various fields that involve growth or decay processes.