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