Home  >  Article  >  Backend Development  >  Detailed explanation of how PHP converts strings into integers and performance test examples

Detailed explanation of how PHP converts strings into integers and performance test examples

伊谢尔伦
伊谢尔伦Original
2017-06-21 11:42:311557browse

In PHP, we can use 3 ways to convert strings into integers.
1. Forced type conversion method
Forced type conversion method is the method of "adding the target type enclosed in parentheses before the variable to be converted".

<?php 
$foo = "1"; // $foo 是字符串类型 
$bar = (int)$foo; // $bar 是整型 
?>

For integer type, Force conversionThe type name is int or integer.
2.Built-in functionMethod
Built-in function method is to use PHP’s built-in function intval to convert variables.

<?php 
$foo = "1"; // $foo 是字符串类型 
$bar = intval($foo); // $bar 是整型 
?>

The format of the intval function is:
int intval(mixed $var [, int $base]);
Although the PHP manual clearly states that intval() cannot be used for array and object Convert. But after my testing, there will be no problems when converting the array. The converted value is 1, not 0 as expected. I'm afraid it's because within PHP, array type variables are also considered to have non-zero values.

When converting object, PHP will give the following notice:
Object of class xxxx could not be converted to int in xxxxx.php on line xx
The conversion value is also 1.
3.Format stringMethod
Format string method is to use sprintf’s %d to format the specified variable to achieve the purpose of type conversion.

<?php 
$foo = "1"; // $foo 是字符串类型 
$bar = sprintf("%d", $foo); // $bar 是字符串类型 
?>

Strictly speaking, the conversion result of sprintf is still of string type, so it should not be regarded as a way to convert a string into an integer. But the string value after his processing has indeed become "an integer that is forced to be converted to a string type."

For ordinary programmers, this is the end of the story, but for some abnormal programmers, let’s talk about the following performance test:

1. Basic functional test
Set the following array:

<?php 
$a[] = "1"; 
$a[] = "a1"; 
$a[] = "1a"; 
$a[] = "1a2"; 
$a[] = "0"; 
$a[] = array(&#39;4&#39;,2); 
$a[] = "2.3"; 
$a[] = "-1"; 
$a[] = new Directory(); 
?>

Use three methods to convert the elements in the array given above in sequence and check the conversion status. The program source code is as follows:

<?php 
$a[] = "1"; 
$a[] = "a1"; 
$a[] = "1a"; 
$a[] = "1a2"; 
$a[] = "0"; 
$a[] = array(&#39;4&#39;,2); 
$a[] = "2.3"; 
$a[] = "-1"; 
$a[] = new Directory(); 
// int 
print "(int)<br />"; 
foreach($a as $v) 
{ 
var_dump((int)$v); 
print "<br />"; 
} 
// intval 
print "intval();<br />"; 
foreach($a as $v) 
{ 
var_dump(intval($v)); 
print "<br />"; 
} 
// sprintf 
print "sprintf();<br />"; 
foreach($a as $v) 
{ 
var_dump(sprintf("%d", $v)); 
print "<br />"; 
} 
?>

The final running result of the program is as follows (the notice that appears when converting the object has been removed):

(int) 
int(1) 
int(0) 
int(1) 
int(1) 
int(0) 
int(1) 
int(2) 
int(-1) 
int(1) 
intval(); 
int(1) 
int(0) 
int(1) 
int(1) 
int(0) 
int(1) 
int(2) 
int(-1) 
int(1) 
sprintf(); 
string(1) "1" 
string(1) "0" 
string(1) "1" 
string(1) "1" 
string(1) "0" 
string(1) "1" 
string(1) "2" 
string(2) "-1" 
string(1) "1"

It can be seen that the results of the three conversions are exactly the same of. So functionally speaking, all three methods are capable of conversion work, so the next step is to see which one is more efficient.

2. Performance test

<?php 
$foo = "1&#39;;Select * ..."; 
?> 
  获取时间点的函数如下(用于获取测试起始点和结束点,以计算消耗时间): 
<?php 
** 
* Simple function to replicate PHP 5 behaviour 
*/ 
function microtime_float() 
{ 
list($usec, $sec) = explode(" ", microtime()); 
return ((float)$usec + (float)$sec); 
} 
?>

The test process is to use each method to convert the variable $foo 1,000,000 times (1 million times), and output the respective consumption time. A total of three sets of tests are conducted. Reduce errors as much as possible. The test program is as follows:

<?php 
function microtime_float() 
{ 
list($usec, $sec) = explode(" ", microtime()); 
return ((float)$usec + (float)$sec); 
} 
$foo = "1&#39;;Select * ..."; 
// (int) 
$fStart = microtime_float(); 
for($i=0;$i<1000000;$i++) 
{ 
$bar = (int)$foo; 
} 
$fEnd = microtime_float(); 
print "(int):" . ($fEnd - $fStart) . "s<br />"; 
// intval() 
$fStart = microtime_float(); 
for($i=0;$i<1000000;$i++) 
{ 
$bar = intval($foo); 
} 
$fEnd = microtime_float(); 
print "intval():" . ($fEnd - $fStart) . "s<br />"; 
// sprintf() 
$fStart = microtime_float(); 
for($i=0;$i<1000000;$i++) 
{ 
$bar = sprintf("%d", $foo); 
} 
$fEnd = microtime_float(); 
print "sprintf():" . ($fEnd - $fStart) . "s<br />"; 
?>

The final test result:

(int):0.67205619812012s 
intval():1.1603000164032s 
sprintf():2.1068270206451s 
(int):0.66051411628723s 
intval():1.1493890285492s 
sprintf():2.1008238792419s 
(int):0.66878795623779s 
intval():1.1613430976868s 
sprintf():2.0976209640503s

It can be seen that using forced type conversion to convert a string into an integer is the fastest.

The above is the detailed content of Detailed explanation of how PHP converts strings into integers and performance test examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn