Fastest Way to Convert String to Integer in PHP

Fastest way to convert string to integer in PHP

I've just set up a quick benchmarking exercise:

Function             time to run 1 million iterations
--------------------------------------------
(int) "123": 0.55029
intval("123"): 1.0115 (183%)

(int) "0": 0.42461
intval("0"): 0.95683 (225%)

(int) int: 0.1502
intval(int): 0.65716 (438%)

(int) array("a", "b"): 0.91264
intval(array("a", "b")): 1.47681 (162%)

(int) "hello": 0.42208
intval("hello"): 0.93678 (222%)

On average, calling intval() is two and a half times slower, and the difference is the greatest if your input already is an integer.

I'd be interested to know why though.


Update: I've run the tests again, this time with coercion (0 + $var)

| INPUT ($x)      |  (int) $x  |intval($x) |  0 + $x   |
|-----------------|------------|-----------|-----------|
| "123" | 0.51541 | 0.96924 | 0.33828 |
| "0" | 0.42723 | 0.97418 | 0.31353 |
| 123 | 0.15011 | 0.61690 | 0.15452 |
| array("a", "b") | 0.8893 | 1.45109 | err! |
| "hello" | 0.42618 | 0.88803 | 0.1691 |
|-----------------|------------|-----------|-----------|

Addendum: I've just come across a slightly unexpected behaviour which you should be aware of when choosing one of these methods:

$x = "11";
(int) $x; // int(11)
intval($x); // int(11)
$x + 0; // int(11)

$x = "0x11";
(int) $x; // int(0)
intval($x); // int(0)
$x + 0; // int(17) !

$x = "011";
(int) $x; // int(11)
intval($x); // int(11)
$x + 0; // int(11) (not 9)

Tested using PHP 5.3.1

How do I convert a string to a number in PHP?

You don't typically need to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type, cast it:

$num = "3.14";
$int = (int)$num;
$float = (float)$num;

In php, how to convert string value into an int?

To convert string to int in PHP, you can use Type Casting method or PHP built-in function intval().

<?php
$string = "56";
$int = intval( $string );
echo $int;
?>

php converting string to integer unexpected behaviour

Casting to an integer using (int) will always cast to the default base, which is 10.

Casting a string to a number this way does not take into account the many ways of formatting an integer value in PHP (leading zero for base 8, leading "0x" for base 16, leading "0b" for base 2). It will simply look at the first characters in a string and convert them to a base 10 integer. Leading zeroes will be stripped off because they have no meaning in numerical values, so you will end up with the decimal value 10 for (int)"010".

Converting an integer value between bases using (int)010 will take into account the various ways of formatting an integer. A leading zero like in 010 means the number is in octal notation, using (int)010 will convert it to the decimal value 8 in base 10.

This is similar to how you use 0x10 to write in hexadecimal (base 16) notation. Using (int)0x10 will convert that to the base 10 decimal value 16, whereas using (int)"0x10" will end up with the decimal value 0: since the "x" is not a numerical value, anything after that will be ignored.

If you want to interpret the string "010" as an octal value, you need to instruct PHP to do so. intval("010", 8) will interpret the number in base 8 instead of the default base 10, and you will end up with the decimal value 8. You could also use octdec("010") to convert the octal string to the decimal value 8. Another option is to use base_convert("010", 8, 10) to explicitly convert the number "010" from base 8 to base 10, however this function will return the string "8" instead of the integer 8.

How to convert string to number in php?

You cast it to an integer.

(int) $Str;

http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

To explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval() function.

Convert String to int php

Simple use http://www.php.net/manual/en/function.ord.php

function num($text)
{
$num=null;

for ($i = 0; $i < strlen($text); $i++)
{
$num =$num.ord($text[$i]);
}

return($num);
}

Example 10183491225753109122105505667. It is 30 digit long number but if you want a unique number then this is it.

Convert string to integer?

Cast your string to an integer explicitly

$id = (int) $id;

ref: http://php.net/manual/en/language.types.type-juggling.php

Converting string to number

0299 is the octal notation of a number, which is very different from the decimal 299. In fact, 9 is invalid in octal and 0299 just has the value 2. Try echo 0299;, then echo 0123;.

If you're treating postcodes as numbers at all, you should strip leading 0s from it and don't use leading 0s in companions:

$code = ltrim($_POST['postcode'], '0');

... $code <= 299 ...


Related Topics



Leave a reply



Submit