What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.



intval — Converts a value to an integer.


int intval(value[, base]);
mixed value: Scalar value to convert to an integer
int base (optional): Specified base for the integer



Integer of the specified base (10 if no base is specified)

intval() attempts to convert a single scalar value to an integer. Integers are whole numbers (such as 1,-2002,34,0, and so on).

If a base argument is provided and the value argument is a string, the value is considered to be of the base specified. See the notes and examples for more details.

The function can convert any normal numeric value, as well as numbers that are represented in scientific notation (such as 1.101e45), as a string (such as "1 cup of flour" or "10,000 keys for a single lock"), in octal base notation 0777or 007), or in hexadecimal base notation0xC0FFEEor0xDE04).


When converting floating-point numbers (doubles), intval() simply drops the fractional value from the number. Due to the way that floating-point numbers are stored, this can sometimes lead to a converted number being one less than expected.

Example 1385. Demonstrate possible gotchas

// Multiply some floating-point numbers by some integers
$value = (1/9) * (1/11) * 9 * 11;

// Display the value - should be 1
print $value . "\n";

// Use intval() to make sure that the value is an int to round down the value - should return 0
// This happens because the number is actually something like 0.999999999999999
// ...however this goes beyond the precision available for PHP and is simply displayed as 1
// When this value is converted to an int, the fractional remainder is dropped, leaving 0
print intval ($value) . "\n";

// Now try the same test with a different order of operations
// The result should be 1
print intval ((1/9) * 9 * (1/11) * 11) . "\n";



PHP Version: 3+, 4+

See also

To convert a value to an integer:


Typecasting (see the PHP Manual)

printf() and sprintf() (using the %d, %x, %X, or %o format specifier). Note that printf() and sprintf() don't actually convert a value to an integer. Instead, they convert the value to a string that looks like an integer. Due to PHP's loose-typing, in many cases a value that looks like an integer is used like an integer. For example,

<?php echo 10 * "99 Red Balloons"; // Will display 990 ?>

To find whether a value is an integer:




Example 1386. Convert a single value to an integer

$value = intval (1.2);

// Do the same thing using typecasting
$value = (int) 1.2;

Example 1387. Demonstrate how the base argument affects the value returned

// Make sure that the first argument is a string
// Note that the value returned is the base10 value of the hex base number
print intval ('F000', 16); // returns 61440

// Make sure that the first argument is a string
// Note that the value returned is the base10 value of the binary number
print intval ('10001101', 2); // returns 141

Example 1388. Convert a bunch of values to integers

// Make a list of the values that we want to convert
$values = array (
    1000 => 1000, '0xFF' => 0xFF, '033' => 033, 'foo' => 'foo', '10,000' => '10,000',
    '1.23456789e10' => 1.23456789e10, '1.22e30' => 1.22e30, '' => ''

// Loop through the values and show what they look like before and after conversion.
// Pay close attention to the 'foo', '10,000', '1.22e30', and '' values
// Notice that only the parts that are recognizable as normal numbers are converted.
// With '1.22e30', the value is completely wrong. This is because the length of
// '1.22e30' goes far past what is allowed for an int. PHP drops the extra bits for the
// value, mangling the sign for the value and leaving us with something like -539222988
foreach ($values as $key => $value)
    printf ("<b>%-20s</b> %d\n", "'$key'", intval ($value));


PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at The authors of this book have elected not to choose any options under the OPL. This online book was obtained from and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.