PHP tanh() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, tanh() returns the hyperbolic tangent of a number.

The hyperbolic tangent is sinh(x) divided by cosh(x). It squeezes any number into the range -1 to 1, which is why it is popular in machine learning.

Syntax

Call tanh() with the values you need. Start with this line.

PHP
$result = tanh(1);
CallWhat it does
tanh(0)Returns 0.
tanh(1)Returns about 0.7616.
tanh(-1)Returns about -0.7616.

tanh() output always stays between -1 and 1. Large positive inputs get very close to 1, and large negative inputs get very close to -1. The inverse function is atanh().

Library declarations
PHP
tanh(float $num): float

Parameters

ParameterDescription
numRequired. The number to calculate the hyperbolic tangent for.

Return Value

Return ValueDescription
floatThe hyperbolic tangent of num, between -1 and 1.

PHP Examples

Example 1 — Basic Hyperbolic Tangent

Try tanh() with a few simple values.

PHP
<?php
echo tanh(0), PHP_EOL;
echo round(tanh(1), 4), PHP_EOL;
echo round(tanh(-1), 4), PHP_EOL;

How It Works

  1. tanh(0) is 0.
  2. tanh(1) is about 0.7616 after rounding.
  3. tanh(-1) is the same number with a negative sign.
  4. The result is always between -1 and 1.

Example 2 — Large Inputs Reach 1 and -1

For big numbers, tanh() gets so close to 1 that PHP shows exactly 1.

PHP
<?php
echo tanh(100), PHP_EOL;
echo tanh(-100), PHP_EOL;
echo tanh(1000), PHP_EOL;

How It Works

  1. tanh(100) is so close to 1 that the float equals 1.
  2. tanh(-100) is the same for the negative side, giving -1.
  3. tanh(1000) is also 1. It does not overflow like sinh().
  4. This makes tanh() safe for large values.

Example 3 — Squash Values into -1 to 1

Loop over several numbers and see how tanh() compresses them.

PHP
<?php
$inputs = [-2, -0.5, 0, 0.5, 2];

foreach ($inputs as $x) {
    echo "tanh($x) = ", round(tanh($x), 4), PHP_EOL;
}

How It Works

  1. Create an array with negative, zero, and positive numbers.
  2. Loop through each number in the array.
  3. Call tanh() and round the result to four decimals.
  4. Every output stays between -1 and 1, and the curve is symmetrical.

Frequently Asked Questions

tanh() returns the hyperbolic tangent of a number, which is sinh($x) / cosh($x). The result is always between -1 and 1.
tan() works with circle angles in radians and can return any number. tanh() works with a hyperbola and always stays between -1 and 1.
The inverse is atanh(), which returns the number whose hyperbolic tangent is the given value.
It smoothly squeezes any input into the range -1 to 1, so it is often used as an activation function in neural networks.
No. The output is limited to -1 to 1, so tanh() never returns INF for normal numbers.

Back to Math Functions

Browse all PHP math function tutorials.

Math functions →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful