w3resource

PHP : intval() function

Description

The intval() function is used to get the integer value of a variable.

Version:

(PHP 4 and above)

Syntax:

intval(var_name, base) 

Parameter:

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 :

<?php
echo intval(102).'<br>';
echo intval(102.22).'<br>';
echo intval('102').'<br>';
echo intval(+102).'<br>';
echo intval(-102).'<br>';
echo intval(0102).'<br>';
echo intval('0002').'<br>';
echo intval(1e20).'<br>';
echo intval(0x1B).'<br>';
echo intval(10200000).'<br>';
echo intval(10200000000000000000).'<br>';
echo intval(10, 2).'<br>';
echo intval('10', 2).'<br>';
?>

Output :

102
102
102
102
-102
66
2
0
27
10200000
0
10
2

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: import_request_ variables
Next: is_array



PHP: Tips of the Day

SQL injection that gets around mysql_real_escape_string()

Consider the following query:

$iId = mysql_real_escape_string("1 OR 1=1");    
$sSql = "SELECT * FROM table WHERE id = $iId";

mysql_real_escape_string() will not protect you against this. The fact that you use single quotes (' ') around your variables inside your query is what protects you against this. The following is also an option:

$iId = (int)"1 OR 1=1";
$sSql = "SELECT * FROM table WHERE id = $iId";

Ref : https://bit.ly/32q3bJ7