Home >Backend Development >PHP Tutorial >How to convert string into int type in php and test results
This article mainly shares with you an article on how to convert a string into an int type in PHP. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to have a look.
Let’s take a look at the background first.
Background and Overview
As early as a few years before Sql injection became rampant, characters Converting strings to integers has been listed as a necessary operation for every web program. The web program forces the id, integer and other values from get or post to be converted into integers through the conversion function, filtering out dangerous characters and minimizing the possibility of the system itself being injected by Sql.
Nowadays, although Sql injection has gradually faded out of the stage of history, in order to ensure the normal operation of web programs, reduce the probability of errors, and better ensure user satisfaction, we also need to Incorrect input is transformed into what we need.
Conversion method
In PHP, we can use 3 methods to convert strings into integers.
1. Forced type conversion method
Forced type conversion method is to "add parentheses before the variable to be converted. target type" (from the "Type Tricks" section of the PHP manual).
The code is as follows:
<?php $foo = "1"; // $foo 是字符串类型 $bar = (int)$foo; // $bar 是整型 ?>
For integer type Say, the cast type name is int or integer.
2. Built-in function method
The built-in function method uses PHP’s built-in function intval to convert variables.
code show as below:
<?php $foo = "1"; // $foo 是字符串类型 $bar = intval($foo); // $bar 是整型 ?>
intval函数的格式为:
int intval(mixed $var [, int $base]); (摘自PHP手册)
虽然PHP手册中明确指出,intval()不能用于array和object的转换。但是经过我测试,转换array的时候不会出任何问题,转换值为1,而不是想象中的0。恐怕是因为在PHP内部,array类型的变量也被认为是非零值得缘故吧。转换object的时候,PHP会给出如下的 notice:
Object of class xxxx could not be converted to int in xxxxx.php on line xx
转换值同样为1。
3.格式化字符串方式
格式化字符串方式,是利用sprintf的%d格式化指定的变量,以达到类型转换的目的。
代码如下:
<?php $foo = "1"; // $foo 是字符串类型 $bar = sprintf("%d", $foo); // $bar 是字符串类型 ?>
严格意义上讲sprintf的转换结果还是string型,因此它不应该算是字符串转化为整数的方式。但是经过他处理之后的字符串值确实已经成为了“被强制转化为字符串类型的整数”。
实际测试
上面介绍了PHP中,将字符串转化为整数的3种方式。对于一般的程序员来说,看到这里就算结束了,下面的部分是针对变态程序员的。
1.基本功能测试
设定以下数组:
代码如下:
<?php $a[] = "1"; $a[] = "a1"; $a[] = "1a"; $a[] = "1a2"; $a[] = "0"; $a[] = array('4',2); $a[] = "2.3"; $a[] = "-1"; $a[] = new Directory(); ?>
使用三种方式依次转化上面给出的数组中的元素,查看转换情况。程序源代码如下:
代码如下:
<?php $a[] = "1"; $a[] = "a1"; $a[] = "1a"; $a[] = "1a2"; $a[] = "0"; $a[] = array('4',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 />"; } ?>
程序的最终运行结果如下(已经去掉转换object时出现的notice):
(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"
由此可以看出,三种转换的结果是完全一样的。那么从功能上讲,3种方式都可以胜任转换工作,那么接下来的工作就是看哪一种效率更高了。
2.性能测试
被测试字符串是我们在注入工作中可能会使用到的一种:
代码如下:
<?php $foo = "1';Select * ..."; ?> 获取时间点的函数如下(用于获取测试起始点和结束点,以计算消耗时间): <?php ** * Simple function to replicate PHP 5 behaviour */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } ?>
测试过程是使用每种方式转换变量$foo 1000000次(100万次),并将各自的消耗时间输出,总共进行三组测试,尽可能降低误差。测试程序如下:
代码如下:
<?php function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $foo = "1';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 />"; ?>
最终的测试结果:
(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
虽然这个测试有点变态(谁会连续转换100w次的整数?),但是由此可以看出,使用强制类型转换将字符串转化为整数速度是最快的。
总结
使用强制类型转换方式将字符串转化为整数是最直接的转化方式之一(可以直接获得整型的变量值)。从代码可读性角度上讲,sprintf方式代码比较长,而且其结果有可能还需要再次进行强制类型转换,而intval函数是典型的面向过程式转换,强制类型转换则比较直接的将“我要转化”这个思想传递给阅读者。从效率上讲,强制类型转换方式也是最快速的转化方式。因此,对于经常进行转化工作的程序员,我推荐使用这种方式。
The above is the detailed content of How to convert string into int type in php and test results. For more information, please follow other related articles on the PHP Chinese website!