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:
- $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 starts with 0, the base is taken as 8
- Otherwise, the base is taken as 10.
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; ?> |
7
Program 2:
<?php $var = 0x423; $int_value = intval($var); echo $int_value; ?> |
1059
Program 3:
<?php $var = "64"; echo intval($var)."\n".intval($var, 8); ?> |
64 52
Reference: http://php.net/manual/en/function.intval.php
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : bansalpiyush177

