36 of 410 menu

The intval Function

The intval function converts the passed value to an integer. The first parameter accepts the value for conversion, the second optional parameter defines the base of the numeral system.

Syntax

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

Example

Convert the string "123" to an integer:

<?php $res = intval("123"); echo $res; ?>

Code execution result:

123

Example

Convert the string "12.3" to an integer:

<?php $res = intval("12.3"); echo $res; ?>

Code execution result:

12

Example

Convert a hexadecimal string with base specification:

<?php $res = intval("1a", 16); echo $res; ?>

Code execution result:

26

Example

Convert an array to an integer:

<?php $res = intval([1, 2, 3]); echo $res; ?>

Code execution result:

0

See Also

  • the floatval function,
    which converts a value to a floating-point number
  • the strval function,
    which converts a value to a string
byenru