intval

Description

Returns the integer value of var,
using the specified base for the conversion
(the default is base 10). intval() should not be used
on objects, as doing so will emit an E_NOTICE level
error and return 1.

Parameters

var

The scalar value being converted to an integer

base

The base for the conversion

Note:

If base is 0, the base used is determined
by the format of var:

if string includes a "0x" (or "0X") prefix, the base is taken
as 16 (hex); otherwise,

if string starts with "0", the base is taken as 8 (octal);
otherwise,

the base is taken as 10 (decimal).

Return Values

The maximum value depends on the system. 32 bit systems have a
maximum signed integer range of -2147483648 to 2147483647. So for example
on such a system, intval('1000000000000') will return
2147483647. The maximum signed integer value for 64 bit systems is
9223372036854775807.

Strings will most likely return 0 although this depends on the
leftmost characters of the string. The common rules of
integer casting
apply.

Appears to be a floating point issue, but the number 1999 is the only number that I was able to get to do this. 19.99 is the price of many things, and for our purpose we must pass it as 1999 instead of 19.99.

You can have it automatically deduce the base of the number from the prefix of the string using the same syntax as integer literals in PHP ("0x" for hexadecimal, "0" for octal, non-"0" for decimal) by passing a base of 0 to intval():

Sometimes intval just won't cut it. For example if you want to use an unsigned 32-bit int and need all 32 bits. Recently, I wrote a little script that took and integer and converted it to an IP address. After realizing I couldn't just mod the whole thing, since the sign bit throws it off (and compensating for that), we ran into a problem where if it was entered into a form, the value somehow wasn't converted to an integer properly, at least not implicitly. The solution for this, and the way I recommend converting a string to an integer, is:

$num = $num + 0;

and PHP will leave your number alone; it'll just know it's a number. Such is the fun of a loosely-typed language. :)

As addendum, the "if ($int > 0)" check in the encode function is redundant. It doesn't do anything bad to keep it in since it will always be true when reaching that point, but it's a meaningless conditional this way. It's a remnant from when I tried to write the function in terms of bitshifts, which could lead to negative ints when shifting if the 32nd bit was set (instead of always padding with 0's when using >> it pads with 1's leading to negative ints).

The behaviour of intval() is interesting when supplying a base, and you better check your intval base-based expressions, as it is counter-intuitive.As the example shows<?php intval('42', 8); // => 34intval(42, 8); // => 42 !?>PHP considers the 42 as being already an integer, and doesn't apply any conversion. And supplying<?php intval(49, 8); // => 49 !?>produces no error and no warning.

Still have on mind, that if you convert big numbers by adding zero, PHP makes automatic "to a float" conversion, so it is same as floatVal(). So if the number is realy big (over 13 digits), you can lose preciosity. Do not use it for such long numbers, if all bits do matter (IPv6 addresses and similar).