How to Represent "Infinity" in PHP?

Representing an infinite number in PHP

In PHP, you can represent "infinity" (i.e. an infinite number) by using the INF or -INF predefined constants. Either of these can be assigned to variables, function arguments, return values, etc.:

$num = INF;

// ...

Both, INF and -INF, are float values:

$num = INF;

var_dump($num); // float(INF)
$num = -INF;

var_dump($num); // float(-INF)

The value INF (i.e. positive infinity) is always greater than any other number:

var_dump(INF > -100); // bool(true)
var_dump(INF > 0); // bool(true)
var_dump(INF > 100); // bool(true)

Similarly, the value -INF (i.e. negative infinity) is always less than any other number:

var_dump(-INF < -100); // bool(true)
var_dump(-INF < 0); // bool(true)
var_dump(-INF < 100); // bool(true)

You can check if a value is infinite or not by using the is_infinite() function, for example, like so:

$num = INF;

var_dump(is_infinite($num)); // bool(true)
$num = -INF;

var_dump(is_infinite($num)); // bool(true)

Hope you found this post useful. It was published . Please show your love and support by sharing this post.