The intval() function is an inbuilt function in PHP which returns the integer value of a variable.
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:
- $var: It is a mandatory parameter serves as the variable which needs to be converted to its integer value.
- $base: It ia a optional parameter specifies the base for conversion of $var to its corresponding integer. If $base is not specified.
- If $var contains 0x (or 0X) as prefix, the base is taken as 16.
- If $var contains 0, the base is taken as 8
- Otherwise, the base is taken as 10.
Return Value: It returns the corresponding integer value of $var.
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: