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

伊谢尔伦
Release: 2023-03-10 22:26:01
Original
1421 people have browsed it

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".

Copy after login

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.

Copy after login

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.

Copy after login

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:

Copy after login

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:

"; 
foreach($a as $v) 
{ 
var_dump((int)$v); 
print "
"; } // intval print "intval();
"; foreach($a as $v) { var_dump(intval($v)); print "
"; } // sprintf print "sprintf();
"; foreach($a as $v) { var_dump(sprintf("%d", $v)); print "
"; } ?>
Copy after login

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"
Copy after login

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

 
  获取时间点的函数如下(用于获取测试起始点和结束点,以计算消耗时间): 
Copy after login

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:

"; 
// intval() 
$fStart = microtime_float(); 
for($i=0;$i<1000000;$i++) 
{ 
$bar = intval($foo); 
} 
$fEnd = microtime_float(); 
print "intval():" . ($fEnd - $fStart) . "s
"; // sprintf() $fStart = microtime_float(); for($i=0;$i<1000000;$i++) { $bar = sprintf("%d", $foo); } $fEnd = microtime_float(); print "sprintf():" . ($fEnd - $fStart) . "s
"; ?>
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!