php函数 要看的

1,microtime -- 返回当前 Unix 时间戳和微秒数<br /> <br /> 1. 用 microtime() 对脚本的运行计时<br /> &lt;?php<br /> /**<br /> * Simple function to replicate PHP 5 behaviour<br /> */<br /> function microtime_float()<br /> {<br /> list($usec, $sec) = explode(&quot; &quot;, 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 &quot;Did nothing in $time seconds\n&quot;;<br /> ?&gt; <br /> <br /> 2,list<br /> list -- 把数组中的值赋给一些变量 <br /> 说明<br /> void list ( mixed varname, mixed ... )<br /> 像 array() 一样,这不是真正的函数,而是语言结构。list() 用一步操作给一组变量进行赋值。 <br /> 注: list() 仅能用于数字索引的数组并假定数字索引从 0 开始。 <br /> 1. list() 例子<br /> &lt;?php<br /> $info = array('coffee', 'brown', 'caffeine');<br /> // Listing all the variables<br /> list($drink, $color, $power) = $info;<br /> echo &quot;$drink is $color and $power makes it special.\n&quot;;<br /> // Listing some of them<br /> list($drink, , $power) = $info;<br /> echo &quot;$drink has $power.\n&quot;;<br /> // Or let's skip to only the third one<br /> list( , , $power) = $info;<br /> echo &quot;I need $power!\n&quot;;<br /> ?&gt; <br /> <br /> 3,substr<br /> substr -- Return part of a string<br /> 例子 1. Basic substr() usage<br /> &lt;?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 &quot;curly braces&quot;<br /> $string = 'abcdef';<br /> echo $string{0}; // a<br /> echo $string{3}; // d<br /> echo $string{strlen($string)-1}; // f<br /> ?&gt; <br /> 例子 2. Using a negative start<br /> &lt;?php<br /> $rest = substr(&quot;abcdef&quot;, -1); // returns &quot;f&quot;<br /> $rest = substr(&quot;abcdef&quot;, -2); // returns &quot;ef&quot;<br /> $rest = substr(&quot;abcdef&quot;, -3, 1); // returns &quot;d&quot;<br /> ?&gt; <br /> 例子 3. Using a negative length<br /> &lt;?php<br /> $rest = substr(&quot;abcdef&quot;, 0, -1); // returns &quot;abcde&quot;<br /> $rest = substr(&quot;abcdef&quot;, 2, -1); // returns &quot;cde&quot;<br /> $rest = substr(&quot;abcdef&quot;, 4, -4); // returns &quot;&quot;<br /> $rest = substr(&quot;abcdef&quot;, -3, -1); // returns &quot;de&quot;<br /> ?&gt; <br /> <br /> __FILE__取得文件绝对路径<br /> <br /> strpos<br /> strpos -- Find position of first occurrence of a string <br /> 例子 1. strpos() examples<br /> <br /> &lt;?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 &quot;The string '$findme' was not found in the string '$mystring'&quot;;<br /> } else {<br /> echo &quot;The string '$findme' was found in the string '$mystring'&quot;;<br /> echo &quot; and exists at position $pos&quot;;<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 /> ?&gt; <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 /> &lt;?php<br /> echo mt_rand() . &quot;\n&quot;;<br /> echo mt_rand() . &quot;\n&quot;;<br /> echo mt_rand(5, 15);<br /> ?&gt; <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 /> &lt;?php<br /> $item = &quot;Zak's Laptop&quot;;<br /> $escaped_item = mysql_escape_string($item);<br /> printf (&quot;Escaped string: %s\n&quot;, $escaped_item);<br /> ?&gt; <br /> 以上例子将产生如下输出: <br /> Escaped string: Zak\'s Laptop<br /> <br /> filemtime<br /> filemtime -- 取得文件修改时间<br /> &lt;?php<br /> $filename = 'somefile.txt';<br /> if (file_exists($filename)) {<br /> echo &quot;$filename was last modified: &quot; . date (&quot;F d Y H:i:s.&quot;, filemtime($filename));<br /> }<br /> ?&gt; <br /> <br /> basename<br /> basename -- 返回路径中的文件名部分<br /> &lt;?php<br /> $path = &quot;/home/httpd/html/index.php&quot;;<br /> $file = basename($path); // $file is set to &quot;index.php&quot;<br /> $file = basename($path,&quot;.php&quot;); // $file is set to &quot;index&quot;<br /> ?&gt; <br /> <br /> filesize<br /> filesize -- 取得文件大小<br /> &lt;?php<br /> // 输出类似:somefile.txt: 1024 bytes<br /> $filename = 'somefile.txt';<br /> echo $filename . ': ' . filesize($filename) . ' bytes';<br /> ?&gt; <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 /> &lt;?php<br /> define(&quot;MAXSIZE&quot;, 100);<br /> echo MAXSIZE;<br /> echo constant(&quot;MAXSIZE&quot;); // same thing as the previous line<br /> ?&gt;
T:0.005730s,M:211.48 KB
返回顶部 留言