views:

140

answers:

7

I need to simulate a ∞ in PHP.

So that min(∞,$number) is always $number.

A: 

I suppose, assuming this is an integer, you could use PHP_INT_MAX constant.

Tim
A: 

Use the constant PHP_INT_MAX.

http://php.net/manual/en/language.types.integer.php

Joe
+3  A: 

I suppose that, for integers, you could use PHP_INT_MAX , the following code :

var_dump(PHP_INT_MAX);

Gives this output, on my machine :

int 2147483647


But you have to be careful ; see Integer overflow (quoting) :

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

And, from the Floating point numbers documentation page :

The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).

Considering the integer overflow, and depending on your case, using this kind of value might be a better (?) solution...

Pascal MARTIN
What if $number if larger than 2147483647?
It will be interpreted as a float, and not an integer (see the portion I quoted about "Integer overflow")
Pascal MARTIN
A: 

You could potentially use the PHP_INT_MAX constant (click for PHP manual docs).

However, you may want to think about whether you really need to use it - it seems like a bit of an odd request.

Daniel May
A: 

min($number, $number + 1) ??

Alix Axel
with int overflow $number + 1 could be < $number
jk
A: 

In Perl you can use

$INF = 9**9E9;

which is larger than any value you can store in IEEE floating point numbers. And that really works as intended: any non-infinite number will be smaller than $INF:

$N < $INF

is true for any "normal" number $N.

Maybe you use it in PHP too?

bart
A: 

min($number,$number) is always $number (also true for max of course)

jk