PHP intval() Function | What is Intval? Get the integer value of a variable
Summary: in this article, you will learn PHP intval() Function | What is Intval? Get the integer value of a variable? let’s understand in details.
What is PHP Intval?
The intval() function is an inbuilt function in PHP that is used to get the integer value of a variable.
It is an inbuilt function, returns the integer value of a variable. Using the specified base for the conversion, furthermore, it returns the integer value of the variable provided. The default base is 10. It shouldn’t be used on objects else it would return E_NOTICE level error and return 1.
Syntax:
Parameter Values
This function accepts two parameters out of which one is mandatory while the other one is optional. Parameters are described below:
| Name | Description | Type |
|---|---|---|
| var_name | It is a mandatory parameter. The scalar value being converted to an integer or Specifies the variable to check. | Mixed* |
| base | It is a optional parameter. it specifies the base to use for the conversion. Only has an effect if the variable is a string. The default base is 10. If base is not specified.
|
Integer |
Return value
The integer value of var on success, or 0 on failure
Technical Details
| Return Value: | The integer value of the variable on success, 0(zero) on failure. furthermore, an empty array will return 0, & a non-empty array will return 1 |
| PHP Changelog: | PHP 5.1: When an object is passed to the variable, it throws E_NOTICE and returns 1 |
| Return Type: | Integer |
| PHP Version: | 4.0+ |
PHP intval() Function Example
Example:1
It will return the integer value of different variables:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $a = 64; echo intval($a) . "<br>"; $b = 6.4; echo intval($b) . "<br>"; $c = "64.4"; echo intval($c) . "<br>"; $d = array(); echo intval($d) . "<br>"; $e = array("yellow", "orange", "red"); echo intval($e) . "<br>"; ?> |
6
64
0
1
Example:2
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php echo intval(146).'<br>'; echo intval(146.22).'<br>'; echo intval('146').'<br>'; echo intval(+146).'<br>'; echo intval(-146).'<br>'; echo intval(0146).'<br>'; echo intval('0004').'<br>'; echo intval(4e60).'<br>'; echo intval(0x2B).'<br>'; echo intval(146000000).'<br>'; echo intval(146000000000000000).'<br>'; echo intval(14, 6).'<br>'; echo intval('14', 6).'<br>'; ?> |
146
146
146
-146
102
4
0
43
146000000
146000000000000000
14
10
Example:3
|
1 2 3 4 5 |
<?php $var = '8.6463'; $int_value = intval($var); echo $int_value; ?> |
Example:4
|
1 2 3 4 5 |
<?php $var = 0x5167; $int_value = intval($var); echo $int_value; ?> |
Example:5
|
1 2 3 4 |
<?php $var = "46"; echo intval($var)."\n".intval($var, 8); ?> |
38
Conclusion:PHP intval
In this article, you have learned What is Intval function, and how to Get the integer value of a variable with example. I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com. I will try to resolve the problem.