w3resource

php.js : intval() function

intval() function

The intval() function is used to get the integer value of a variable with an optional base for the conversion.

Version:

1103.1210

Syntax:

intval(var_name, base)

Parameters:

Name Description Required /
Optional
Type
var_name The scalar value being converted to an integer Required Mixed*
base The base for the conversion. (default is base 10) Optional Integer

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value:

The integer value of var on success, or 0 on failure

Value Type: Integer.

Example of php.js intval() function:

In the following web document intval() function is used to get the integer value of different variables.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>php.js : intval() function example</title>
<script type="text/javascript" src="../../phpjs/variable/variable.min.js"> </script>
</head>
<body>
<h1 style="color: green">php.js : intval() function example</h1>
<h3>Get the integer value of a variable</h3>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
document.write(intval(102)+'<br>');
document.write(intval(102.22)+'<br>');
document.write(intval('102')+'<br>');
document.write(intval(+102)+'<br>');
document.write(intval(-102)+'<br>');
document.write(intval(0102)+'<br>');
document.write(intval('0002')+'<br>');
document.write(intval(1e20)+'<br>');
document.write(intval(0x1B)+'<br>');
document.write(intval(10200000)+'<br>');
document.write(intval(10200000000000000000)+'<br>');
document.write(intval(10, 2)+'<br>');
document.write(intval('10', 2)+'<br>');
//]]>
</script>
</body>
</html>

Output of the function:

102
102
102
102
-102
66
2
100000000000000000000
27
10200000
10200000000000000000
10
2

View example of php.js intval() function in browser

Download phpjs.zip

Previous: php.js : gettype() function
Next: php.js : is_array() function