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

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

Example

Convert the string "123" to an integer:

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

Code execution result:

php123

Example

Convert the string "12.3" to an integer:

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

Code execution result:

php12

Example

Convert a hexadecimal string with base specification:

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

Code execution result:

php26

Example

Convert an array to an integer:

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

Code execution result:

php0

See Also

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