PHP100 2019-03-27
代码如下:
<?php $foo = "1"; // $foo 是字符串类型 $bar = (int)$foo; // $bar 是整型 ?>
代码如下:
<?php $foo = "1"; // $foo 是字符串类型 $bar = intval($foo); // $bar 是整型 ?>
代码如下:
<?php $foo = "1"; // $foo 是字符串类型 $bar = sprintf("%d", $foo); // $bar 是字符串类型 ?>
代码如下:
<?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 />"; } ?>
代码如下:
<?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); } ?>
代码如下:
<?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 />"; ?>
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh