Member Login:


PHP: Converting A String To Integer

Document Reference: TN200906001 - Rev: 4.44 - Last Update: 26-06-2014 08:50 GMT - Downloaded: 27-May-2019 15:06 GMT

The intval() variable handling function can be used to convert a string to a integer value. A base parameter for the conversion is optional.

Syntax

With optional base parameter:
$integer = intval($any_variable, $base);
Without optional base parameter:
$integer = intval($any_variable);

$integer

The returned integer value on successful conversion, or 0 on failure. Empty arrays will return 0 and non-empty arrays will return 1. See examples below.

$any_variable (var parameter)

Required parameter that accepts multiple data types. It is the scalar value being converted to an integer.

$base (base parameter)

Optional parameter as integer data type. It is the base for the conversion. Default base is 10 and has no effect unless the var parameter is a string. See examples below.

Examples

String Conversion

Without optional base parameter:
$integer = intval($any_variable);
$any_variablestatement$integer
"37"$integer = intval("37");37
"+37"$integer = intval("+37");37
"-37"$integer = intval("-37");-37
"037"$integer = intval("037");37
"0x25"$integer = intval("0x25");0
"37.5"$integer = intval("37.5");37
"37 coins"$integer = intval("37 coins");37
"37%"$integer = intval("37%");37
"37C"$integer = intval("37C");37
"X37"$integer = intval("X37");0
With optional base parameter:
$integer = intval($any_variable, $base);
$any_variable$basestatement$integer
"37"2$integer = intval("37", 2);0 1)
"100101"2$integer = intval("100101", 2);37
"37"8$integer = intval("37", 8);31
"45"8$integer = intval("45", 8);37
"37"10$integer = intval("37", 10);37
"37"16$integer = intval("37", 16);55
"25"16$integer = intval("25", 16);37
"00"16$integer = intval("00", 16);0
"ff"16$integer = intval("ff", 16);255

1) Unsuccessful conversion as 37 is not a valid binary number.

Number Conversion

Without optional base parameter:
$integer = intval($any_variable);
$any_variablestatement$integer
37$integer = intval(37);37
-37$integer = intval(-37);-37
37.5$integer = intval(37.5);37
037$integer = intval(037);31 1)
045$integer = intval(045);37 1)
0x37$integer = intval(0x37);55 2)
0x25$integer = intval(0x25);37 2)

1) Value for $any_variable precedes with 0 indicating octal (base 8) notation.
2) Value for $any_variable precedes with 0x indicating hexadecimal (base 16) notation.

With optional base parameter:
$integer = intval($any_variable, $base);
$any_variable$basestatement$integer
372$integer = intval(37, 2);37 1)
378$integer = intval(37, 8);37 1)
3710$integer = intval(37, 10);37 1)
3716$integer = intval(37, 16);37 1)

1) The base parameter has no effect as the var parameter is not a string.

Array Conversion

Without optional base parameter:
$integer = intval($any_variable);
$any_variablestatement$integer
array()$integer = intval(array());0
array("x", "y", "z")$integer = intval(array("x", "y", "z"));1

This website uses cookies to give you the best experience on our website, to personalise content and to analyse our website traffic. Some cookies may have been set already. To find out more about our use of cookies you can visit our Privacy Statement. By browsing this website, you agree to our use of cookies.

Hide this message