-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrrev.php
More file actions
43 lines (40 loc) · 855 Bytes
/
strrev.php
File metadata and controls
43 lines (40 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
<?php
$str = 'abcdefghijklmnopqrstuvwx';
$j = 0;
$time = microtime(true);
function reverse($str='') {
$len = strlen($str);
$mid = floor($len/2);
for ($i=0; $i<$mid; $i++) {
$temp = $str[$i];
$str[$i] = $str[$len-$i-1];
$str[$len-$i-1] = $temp;
}
return $str;
}
function reverse01($str='') {
$result = '';
$len = strlen($str);
for ($i=1; $i<=$len; $i++) {
$result .= substr($str, -$i, 1);
}
return $result;
}
function reverse02($str='') {
static $result = '';
/* 用堆栈来理解递归调用 */
if (strlen($str) > 0) {
reverse(substr($str, 1));
$result .= substr($str, 0, 1);//此句必须放在上一语句之后
}
return $result;
}
$a = 'abc';
while($j < 1000000){
reverse02($a);
$j++;
}
echo reverse($a),"<br/>";
echo microtime(true) - $time;
echo "<br/>";