PHP intval() function

In this chapter you will learn:

  1. Description for PHP intval() function
  2. Syntax for PHP intval() function
  3. Parameter for PHP intval() function
  4. Return value for PHP intval() function
  5. Example - Find out the int value

Description

Returns the integer value of var, using the specified base for the conversion (the default is base 10).

Syntax

PHP intval() function has the following syntax.

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

Parameter

  • var - The scalar value being converted to an integer
  • base - The base for the conversion

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

The integer value of var on success, or 0 on failure.

Example

Find out the int value


<?php/*from  j  ava2s. c  o m*/
echo intval(2) . "\n";                       // 2
echo intval(4.1) . "\n";                     // 4
echo intval('4') . "\n";                     // 4
echo intval('+42') . "\n";                   // 42
echo intval('-42') . "\n";                   // -42
echo intval(042) . "\n";                     // 34
echo intval('04') . "\n";                    // 4
echo intval(1e10) . "\n";                    // 1410065408
echo intval('1e10') . "\n";                  // 1
echo intval(0x1A) . "\n";                    // 26
echo intval(42, 8) . "\n";                   // 42
echo intval('42', 8) . "\n";                 // 34
echo intval(array()) . "\n";                 // 0
echo intval(array('foo', 'bar')) . "\n";     // 1
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP is_array(value) function
  2. Syntax for PHP is_array(value) function
  3. Parameter for
  4. Return value for
  5. Example - Is it an array
Home » PHP Tutorial » PHP Data Type Functions
PHP boolval() function
PHP floatval() function
PHP gettype() function
PHP intval() function
PHP is_array(value) function
PHP is_bool() function
PHP is_float() function
PHP is_int() function
PHP is_null() function
PHP is_numeric() function
PHP is_object() function
PHP is_resource() function
PHP is_string() function
PHP isset() function
PHP settype() function
PHP strval() function