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)
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.