W3docs

tanh()

Today, we will discuss the tanh() function in PHP. This function is used to calculate the hyperbolic tangent of a number.

The PHP tanh() function returns the hyperbolic tangent of a number. Unlike the ordinary tan(), which works with angles on a circle, tanh() works with hyperbolic angles and always returns a value strictly between -1 and 1. That bounded, smooth S-shaped output is why it shows up in neural networks, physics, and signal processing.

This page covers the syntax, the math behind it, its domain and range, edge cases, and how it relates to the other hyperbolic functions.

Syntax

tanh(float $num): float
  • $num — the value (in radians, interpreted as a hyperbolic angle) whose hyperbolic tangent you want.
  • Return value — the hyperbolic tangent of $num, a float in the open interval (-1, 1).

The math: what tanh actually computes

The hyperbolic tangent is the ratio of the hyperbolic sine to the hyperbolic cosine:

tanh(x) = sinh(x) / cosh(x) = (e^x - e^-x) / (e^x + e^-x)

That definition gives tanh() a few properties worth remembering:

  • tanh(0) is exactly 0.
  • It is an odd function: tanh(-x) == -tanh(x).
  • As x grows, the output approaches 1; as x falls, it approaches -1 — but it never reaches them for any finite input.

Basic example

php— editable, runs on the server

This prints 0.46211715726001. We pass 0.5 to tanh() and echo the returned float.

Domain and range

The domain of tanh() is every real number — you can pass any float without an error. The range is the open interval (-1, 1). The table below shows how the output saturates toward the edges:

<?php
foreach ([-INF, -2, -1, 0, 1, 2, INF] as $x) {
    printf("tanh(%s) = %s\n", $x, tanh($x));
}
?>

Output:

tanh(-INF) = -1
tanh(-2) = -0.96402758007582
tanh(-1) = -0.76159415595576
tanh(0) = 0
tanh(1) = 0.76159415595576
tanh(2) = 0.96402758007582
tanh(INF) = 1

Notice the symmetry around 0 (the odd-function property) and how quickly the result flattens out: by x = 2 it is already past 0.96.

A practical use: a squashing function

Because tanh() maps any number into (-1, 1), it is a common activation function that "squashes" an unbounded score into a bounded, signed value:

<?php
// Map raw scores to a value between -1 and 1
$scores = [-5.0, -1.0, 0.0, 1.0, 5.0];

foreach ($scores as $score) {
    printf("%5.1f -> %+.4f\n", $score, tanh($score));
}
?>

Output:

 -5.0 -> -0.9999
 -1.0 -> -0.7616
  0.0 -> +0.0000
  1.0 -> +0.7616
  5.0 -> +0.9999

Large positive inputs land near +1, large negative inputs near -1, and 0 stays at 0 — exactly the behavior you want from a centered, bounded normalizer.

tanh() is one of PHP's hyperbolic math functions. You often use them together:

  • sinh() — hyperbolic sine, the numerator in the formula above.
  • cosh() — hyperbolic cosine, the denominator.
  • atanh() — the inverse, hyperbolic arc tangent (takes a value in (-1, 1) and returns the original number).
  • tan() — the ordinary (circular) tangent, for angles rather than hyperbolic angles.

Conclusion

tanh() returns the hyperbolic tangent of a number: a smooth, odd function whose output is always between -1 and 1. Pass any real number, get a bounded result that saturates toward ±1 for large magnitudes. That makes it a natural fit for normalizing unbounded values, and it pairs with sinh(), cosh(), and its inverse atanh() for hyperbolic math.

Practice

Practice
What does the PHP 'tanh' function do?
What does the PHP 'tanh' function do?
Was this page helpful?