English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP intval() function usage and example

PHP available functions

intval() The function is used to get the integer value of the variable.

intval() The PHP function converts the variable var to an integer value using the specified base (default is decimal), and returns the integer value of the variable var. intval() cannot be used for objects, otherwise it will produce an E_NOTICE error and return 1.

PHP 4, PHP 5, PHP 7

Syntax

int intval ( mixed $var [, int $base = 10 ] )

Parameter description:

  • $var: The numeric value to be converted to an integer.
  • $base: The base used for conversion.

If base is 0, the format of var is detected to determine the used base:

  • If the string includes the prefix "0x" (or "0X"), use 16 base (hex); otherwise,
  • If the string starts with "0", use 8 base (octal); otherwise,
  • will be used 10 base (decimal).

Return value

Returns the integer value of var on success, and 0 on failure. An empty array returns 0, and a non-empty array returns 1.

The maximum value depends on the operating system. 32 bit system, the maximum signed integer range is -2147483648 to 2147483647. For example, on such a system, intval('1000000000000 2147483647.64 On a bit system, the maximum signed integer value is 9223372036854775807.

A string may return 0, although it depends on the character at the leftmost position of the string.

Online Example

<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
?>

PHP available functions