1,microtime -- 返回当前 Unix 时间戳和微秒数<br />
<br />
1. 用 microtime() 对脚本的运行计时<br />
<?php<br />
/**<br />
* Simple function to replicate PHP 5 behaviour<br />
*/<br />
function microtime_float()<br />
{<br />
list($usec, $sec) = explode(" ", microtime());<br />
return ((float)$usec + (float)$sec);<br />
}<br />
$time_start = microtime_float();<br />
// Sleep for a while<br />
usleep(100);<br />
$time_end = microtime_float();<br />
$time = $time_end - $time_start;<br />
echo "Did nothing in $time seconds\n";<br />
?> <br />
<br />
2,list<br />
list -- 把数组中的值赋给一些变量 <br />
说明<br />
void list ( mixed varname, mixed ... )<br />
像 array() 一样,这不是真正的函数,而是语言结构。list() 用一步操作给一组变量进行赋值。 <br />
注: list() 仅能用于数字索引的数组并假定数字索引从 0 开始。 <br />
1. list() 例子<br />
<?php<br />
$info = array('coffee', 'brown', 'caffeine');<br />
// Listing all the variables<br />
list($drink, $color, $power) = $info;<br />
echo "$drink is $color and $power makes it special.\n";<br />
// Listing some of them<br />
list($drink, , $power) = $info;<br />
echo "$drink has $power.\n";<br />
// Or let's skip to only the third one<br />
list( , , $power) = $info;<br />
echo "I need $power!\n";<br />
?> <br />
<br />
3,substr<br />
substr -- Return part of a string<br />
例子 1. Basic substr() usage<br />
<?php<br />
echo substr('abcdef', 1); // bcdef<br />
echo substr('abcdef', 1, 3); // bcd<br />
echo substr('abcdef', 0, 4); // abcd<br />
echo substr('abcdef', 0, 8); // abcdef<br />
echo substr('abcdef', -1, 1); // f<br />
// Accessing single characters in a string<br />
// can also be achived using "curly braces"<br />
$string = 'abcdef';<br />
echo $string{0}; // a<br />
echo $string{3}; // d<br />
echo $string{strlen($string)-1}; // f<br />
?> <br />
例子 2. Using a negative start<br />
<?php<br />
$rest = substr("abcdef", -1); // returns "f"<br />
$rest = substr("abcdef", -2); // returns "ef"<br />
$rest = substr("abcdef", -3, 1); // returns "d"<br />
?> <br />
例子 3. Using a negative length<br />
<?php<br />
$rest = substr("abcdef", 0, -1); // returns "abcde"<br />
$rest = substr("abcdef", 2, -1); // returns "cde"<br />
$rest = substr("abcdef", 4, -4); // returns ""<br />
$rest = substr("abcdef", -3, -1); // returns "de"<br />
?> <br />
<br />
__FILE__取得文件绝对路径<br />
<br />
strpos<br />
strpos -- Find position of first occurrence of a string <br />
例子 1. strpos() examples<br />
<br />
<?php<br />
$mystring = 'abc';<br />
$findme = 'a';<br />
$pos = strpos($mystring, $findme);<br />
// Note our use of ===. Simply == would not work as expected<br />
// because the position of 'a' was the 0th (first) character.<br />
if ($pos === false) {<br />
echo "The string '$findme' was not found in the string '$mystring'";<br />
} else {<br />
echo "The string '$findme' was found in the string '$mystring'";<br />
echo " and exists at position $pos";<br />
}<br />
// We can search for the character, ignoring anything before the offset<br />
$newstring = 'abcdef abcdef';<br />
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0<br />
?> <br />
<br />
mt_rand<br />
mt_rand -- 生成更好的随机数<br />
例子 1. mt_rand() 范例<br />
说明<br />
int mt_rand ( [int min, int max] )<br />
很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc 随机数发生器。mt_rand() 函数是非正式用来替换它的。该函数用了 Mersenne Twister 中已知的特性作为随机数发生器,它可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。 <br />
如果没有提供可选参数 min 和 max,mt_rand() 返回 0 到 RAND_MAX 之间的伪随机数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用 mt_rand(5, 15)。 <br />
<?php<br />
echo mt_rand() . "\n";<br />
echo mt_rand() . "\n";<br />
echo mt_rand(5, 15);<br />
?> <br />
上例的输出类似于:<br />
1604716014<br />
1478613278<br />
6<br />
<br />
<br />
bin2hex<br />
bin2hex -- 将二进制数据转换成十六进制表示 <br />
<br />
mysql_escape_string<br />
mysql_escape_string -- 转义一个字符串用于 mysql_query <br />
例子 1. mysql_escape_string() 例子<br />
<?php<br />
$item = "Zak's Laptop";<br />
$escaped_item = mysql_escape_string($item);<br />
printf ("Escaped string: %s\n", $escaped_item);<br />
?> <br />
以上例子将产生如下输出: <br />
Escaped string: Zak\'s Laptop<br />
<br />
filemtime<br />
filemtime -- 取得文件修改时间<br />
<?php<br />
$filename = 'somefile.txt';<br />
if (file_exists($filename)) {<br />
echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));<br />
}<br />
?> <br />
<br />
basename<br />
basename -- 返回路径中的文件名部分<br />
<?php<br />
$path = "/home/httpd/html/index.php";<br />
$file = basename($path); // $file is set to "index.php"<br />
$file = basename($path,".php"); // $file is set to "index"<br />
?> <br />
<br />
filesize<br />
filesize -- 取得文件大小<br />
<?php<br />
// 输出类似:somefile.txt: 1024 bytes<br />
$filename = 'somefile.txt';<br />
echo $filename . ': ' . filesize($filename) . ' bytes';<br />
?> <br />
<br />
<br />
<br />
urlencode<br />
urlencode -- 编码 URL 字符串<br />
urldecode<br />
urldecode -- 解码已编码的 URL 字符串<br />
<br />
<br />
<br />
constant<br />
constant -- Returns the value of a constant<br />
例子 1. constant() example<br />
<?php<br />
define("MAXSIZE", 100);<br />
echo MAXSIZE;<br />
echo constant("MAXSIZE"); // same thing as the previous line<br />
?>