Open In App

PHP | intval() Function

The intval() function is an inbuilt function in PHP which returns the integer value of a variable.

Syntax:



int intval ( $var, $base )

Parameters: This function accepts two parameters out of which one is mandatory while the other one is optional. Parameters are described below:

Return Value: It returns the corresponding integer value of $var.



Examples:

Input : $var = '120', $base = 8
Output : 80

Input : $var = 0x34
Output : 52

Input : $var = 034
Output : 28

Below programs illustrate the use of intval() function in PHP:

Program 1:




<?php
$var = '7.423';
$int_value = intval($var);
echo $int_value;
?>

Output:
7

Program 2:




<?php
$var = 0x423;
$int_value = intval($var);
echo $int_value;
?>

Output:
1059

Program 3:




<?php
$var = "64";
echo intval($var)."\n".intval($var, 8);
  
?>

Output:
64
52

Reference: http://php.net/manual/en/function.intval.php


Article Tags :