PHP intval() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, intval() converts a value to an integer and returns it. Decimal parts are cut off, not rounded, and text is read from the start.

intval() is the function form of the (int) cast, with one extra feature. When the value is a string, you can pass a base, such as 2, 8 or 16, to read binary, octal or hexadecimal numbers. If a string does not start with a number, the result is 0.

Syntax

Call intval() with the values you need. Start with this line.

PHP
$number = intval($value);
CallWhat it does
intval("42")Returns 42, the string converted to an integer.
intval(42.99)Returns 42. The decimal part is cut off, not rounded.
intval("ff", 16)Returns 255, reading the string as a hexadecimal number.

The base only works when the value is a string. For numbers, it is ignored. With a base of 0, PHP checks the prefix of the string: 0x means hex, 0 means octal and 0b means binary, and anything else is decimal.

Library declarations
PHP
intval(mixed $value, int $base = 10): int

Parameters

ParameterDescription
valueRequired. The value to convert. Strings are read from the start, up to the first character that cannot belong to a number.
baseOptional. The number base used when the value is a string. The default is 10. Use 0 to detect the base from the prefix, or any value from 2 to 36.

Return Value

Return ValueDescription
intThe integer value of the input. Strings that do not start with a number return 0. Empty arrays return 0 and non-empty arrays return 1.

PHP Examples

Example 1 — Converting Strings and Floats

Turn common values into integers and print them with echo.

PHP
<?php
echo intval("42"), PHP_EOL;
echo intval(42.99), PHP_EOL;
echo intval("12abc"), PHP_EOL;
echo intval("abc"), PHP_EOL;

How It Works

  1. "42" is a clean number string, so the result is 42.
  2. 42. 99 is a float. intval() cuts off the decimal part and returns 42, without rounding up.
  3. "12abc" starts with 12, so PHP keeps 12 and ignores the letters.
  4. "abc" does not start with a number, so the result is 0.

Example 2 — Using a Base

The second argument lets intval() read binary, octal and hexadecimal text.

PHP
<?php
echo intval("101", 2), PHP_EOL;
echo intval("ff", 16), PHP_EOL;
echo intval("0x1A", 16), PHP_EOL;
echo intval("012", 0), PHP_EOL;
echo intval("0x1A", 0), PHP_EOL;
echo intval("42", 8), PHP_EOL;

How It Works

  1. "101" read as base 2 (binary) is 5.
  2. "ff" read as base 16 (hexadecimal) is 255, and the 0x prefix is also accepted for base 16.
  3. With base 0, PHP looks at the prefix: "012" starts with 0, so it is octal and equals 10, and "0x1A" starts with 0x, so it is hex and equals 26.
  4. "42" read as base 8 (octal) is 4 * 8 + 2 = 34.

Example 3 — Truncation and Special Values

See how intval() treats negative floats, scientific notation, null, booleans and arrays.

PHP
<?php
echo intval(-3.9), PHP_EOL;
echo intval("1e3"), PHP_EOL;
echo intval(null), PHP_EOL;
echo intval(true), PHP_EOL;
echo intval([]), PHP_EOL;
echo intval([5]), PHP_EOL;

How It Works

  1. -3. 9 is truncated toward zero, so the result is -3, not -4.
  2. "1e3" is scientific notation for 1000, and PHP reads it as a number.
  3. null becomes 0, and true becomes 1.
  4. An empty array becomes 0, and any non-empty array becomes 1, no matter what it holds.

Frequently Asked Questions

intval() converts a value to an integer. Decimals are cut off, strings are read from the start, and values that cannot be read as a number become 0.
No. It truncates toward zero, so intval(9.99) is 9 and intval(-9.99) is -9. Use round(), floor() or ceil() first if you need rounding.
Pass the base as the second argument: intval("ff", 16) returns 255 and intval("101", 2) returns 5. With base 0, PHP picks the base from the prefix (0x, 0b or 0).
For most values they are the same. intval() is a function, so you can pass it to array_map(), and it accepts a base for strings. The (int) cast is shorter and has no base option.
It returns 0, with no warning or error. Use is_numeric() or filter_var() with FILTER_VALIDATE_INT first if you need to know whether the input was a real number.

Next: is_array()

is_array() checks whether a variable is an array. It returns true for arrays and false for.

is_array() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful