intval
Converts a value to an integer, optionally interpreting a string in another base.
intval(mixed $value, int $base = 10): int- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The value being converted to an integer. |
$baseoptional | int= 10 | The base for the conversion, between 2 and 36, or 0 to detect the base from a 0x, 0b or 0 prefix. Ignored unless $value is a string. |
Return value
intThe integer value of $value on success, or 0 on failure. Empty arrays return 0 and non-empty arrays return 1.
Errors
Passing an object raises an E_WARNING (E_NOTICE before PHP 8.0) and returns 1. A $base outside 2-36 (other than 0) throws a ValueError.
Changelog
- PHP 8.0.0The error level raised when converting from an object changed from E_NOTICE to E_WARNING.
intval() is the (int) cast plus one extra power: a base. For strings it reads an optional sign and then as many valid digits as it can, stopping at the first character that does not fit, so '42abc' becomes 42 and 'abc42' becomes 0.
Floats are truncated toward zero, never rounded: 4.9 is 4 and -4.9 is -4. A numeric string bigger than PHP_INT_MAX saturates at PHP_INT_MAX rather than wrapping.
The base argument
$base only applies when $value is a string — intval(0x1A, 8) is 26, because the literal was already an int before the call. Pass 16 for hex, 2 for binary, 8 for octal, or 0 to let PHP detect the base from a 0x, 0b or leading 0 prefix.
When to reach for it
Use it to coerce trusted, loosely typed input — query string pages, CSV columns, config values — into a real int. Use it with an explicit base when decoding hex colours, permission masks or binary flags stored as text.
Do not use it to validate. A failed parse and a genuine zero both come back as 0. When the value must actually be an integer, run filter_var($v, FILTER_VALIDATE_INT) first, which returns false when it is not.
Example
When to use it
- Turning a ?page= query-string value into a safe positive integer before building a LIMIT clause
- Decoding a hex colour component or a text-stored permission mask with intval($s, 16) or intval($s, 8)
- Normalising numeric strings coming out of a CSV import or a legacy database column
Frequently asked questions
Why does the base argument have no effect on my value?
Because $base only applies when $value is a string. intval(0x1A, 8) returns 26 since the hex literal was already an int before the call.
Does intval() round or truncate floats?
It truncates toward zero, so intval(4.99) is 4 and intval(-4.99) is -4. Use round(), floor() or ceil() first if you need different behaviour.
How do I tell whether intval() actually parsed a number?
You cannot from the result — invalid input and a real zero both return 0. Validate first with filter_var($v, FILTER_VALIDATE_INT), which returns false on failure.
More examples
Converting other bases
With base 0 the prefix decides: '0b1010' is read as binary (10) and '012' as octal (10). The last line shows why an explicit base matters — '42' read as octal is 4*8+2 = 34, not 42.
<?php
echo intval('ff', 16), "\n";
echo intval('1010', 2), "\n";
echo intval('755', 8), "\n";
echo intval('0b1010', 0), "\n";
echo intval('012', 0), "\n";
echo intval('42', 8), "\n";Sanitising pagination input
'25 per page' converts to 25 because intval() stops at the space. The empty offset becomes 0, and the max()/min() clamps turn any garbage-to-zero conversion into a safe default rather than a broken query.
<?php
$query = ['page' => '3', 'limit' => '25 per page', 'offset' => ''];
$page = max(1, intval($query['page']));
$limit = min(100, max(1, intval($query['limit'])));
$offset = intval($query['offset']);
printf("page=%d limit=%d offset=%d\n", $page, $limit, $offset);Failure looks exactly like zero
'0', 'abc' and '' all produce intval() 0, so the result cannot tell you whether the parse succeeded. filter_var() returns false for the two invalid strings. The huge number saturates at PHP_INT_MAX instead of overflowing.
<?php
foreach (['0', 'abc', '', '9999999999999999999999'] as $v) {
printf("%-24s intval=%-20d valid=%s\n",
var_export($v, true),
intval($v),
var_export(filter_var($v, FILTER_VALIDATE_INT), true)
);
}
Discussion