麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > PHP > 正文

8個(gè)必備的PHP功能開(kāi)發(fā)

2020-03-22 20:32:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
PHP開(kāi)發(fā)的程序員應(yīng)該清楚,PHP中有很多內(nèi)置的功能,掌握了它們,可以幫助你在做PHP開(kāi)發(fā)時(shí)更加得心應(yīng)手,本文將分享8個(gè)開(kāi)發(fā)必備的PHP功能,個(gè)個(gè)都非常實(shí)用,希望各位PHP開(kāi)發(fā)者能夠掌握。1、傳遞任意數(shù)量的函數(shù)參數(shù)
我們?cè)?NET或者JAVA編程中,一般函數(shù)參數(shù)個(gè)數(shù)都是固定的,但是PHP允許你使用任意個(gè)數(shù)的參數(shù)。下面這個(gè)示例向你展示了html' target='_blank'>PHP函數(shù)的默認(rèn)參數(shù):
// 兩個(gè)默認(rèn)參數(shù)的函數(shù) function foo($arg1 = ”, $arg2 = ”) { echo “arg1: $arg1/n”; echo “arg2: $arg2/n”; foo(‘hello','world'); /* 輸出:arg1: helloarg2: worldfoo(); /* 輸出:arg1:arg2:下面這個(gè)示例是PHP的不定參數(shù)用法,其使用到了 func_get_args()方法:
// 是的,形參列表為空 function foo() { // 取得所有的傳入?yún)?shù)的數(shù)組 $args = func_get_args(); foreach ($args as $k = $v) { echo “arg”.($k+1).”: $v/n”; foo(); /* 什么也不會(huì)輸出 */ foo(‘hello'); /* 輸出arg1: hellofoo(‘hello', ‘world', ‘a(chǎn)gain'); /* 輸出arg1: helloarg2: worldarg3: again
2、使用glob()查找文件
大部分PHP函數(shù)的函數(shù)名從字面上都可以理解其用途,但是當(dāng)你看到 glob() 的時(shí)候,你也許并不知道這是用來(lái)做什么的,其實(shí)glob()和scandir() 一樣,可以用來(lái)查找文件,請(qǐng)看下面的用法:
// 取得所有的后綴為PHP的文件 $files = glob(‘*.php'); print_r($files); /* 輸出:Array[0] = phptest.php[1] = pi.php[2] = post_output.php[3] = test.php你還可以查找多種后綴名:// 取PHP文件和TXT文件 $files = glob(‘*.{php,txt}', GLOB_BRACE); print_r($files); /* 輸出:Array[0] = phptest.php[1] = pi.php[2] = post_output.php[3] = test.php[4] = log.txt[5] = test.txt你還可以加上路徑:
$files = glob(‘../images/a*.jpg'); print_r($files); /* 輸出:Array[0] = ../images/apple.jpg[1] = ../images/art.jpg如果你想得到絕對(duì)路徑,你可以調(diào)用 realpath() 函數(shù):
$files = glob(‘../images/a*.jpg'); // applies the function to each array element $files = array_map(‘realpath',$files); print_r($files); /* output looks like:Array[0] = C:/wamp/www/images/apple.jpg[1] = C:/wamp/www/images/art.jpg3、獲取內(nèi)存使用情況信息
PHP的內(nèi)存回收機(jī)制已經(jīng)非常強(qiáng)大,你也可以使用PHP腳本獲取當(dāng)前內(nèi)存的使用情況,調(diào)用memory_get_usage() 函數(shù)獲取當(dāng)期內(nèi)存使用情況,調(diào)用memory_get_peak_usage() 函數(shù)獲取內(nèi)存使用的峰值。參考代碼如下:
echo “Initial: “.memory_get_usage().” bytes /n”; /* 輸出Initial: 361400 bytes// 使用內(nèi)存 for ($i = 0; $i 100000; $i++) { $array []= md5($i); // 刪除一半的內(nèi)存 for ($i = 0; $i 100000; $i++) { unset($array[$i]); echo “Final: “.memory_get_usage().” bytes /n”; /* printsFinal: 885912 bytesecho “Peak: “.memory_get_peak_usage().” bytes /n”; /* 輸出峰值Peak: 13687072 bytes4、獲取CPU使用情況信息
獲取了內(nèi)存使用情況,也可以使用PHP的 getrusage()獲取CPU使用情況,該方法在windows下不可用。
print_r(getrusage()); /* 輸出Array[ru_oublock] = 0[ru_inblock] = 0[ru_msgsnd] = 2[ru_msgrcv] = 3[ru_maxrss] = 12692[ru_ixrss] = 764[ru_idrss] = 3864[ru_minflt] = 94[ru_majflt] = 0[ru_nsignals] = 1[ru_nvcsw] = 67[ru_nivcsw] = 4[ru_nswap] = 0[ru_utime.tv_usec] = 0[ru_utime.tv_sec] = 0[ru_stime.tv_usec] = 6269[ru_stime.tv_sec] = 0這個(gè)結(jié)構(gòu)看上出很晦澀,除非你對(duì)CPU很了解。下面一些解釋?zhuān)?
ru_oublock: 塊輸出操作
ru_inblock: 塊輸入操作
ru_msgsnd: 發(fā)送的message
ru_msgrcv: 收到的message
ru_maxrss: 最大駐留集大小
ru_ixrss: 全部共享內(nèi)存大小
ru_idrss:全部非共享內(nèi)存大小
ru_minflt: 頁(yè)回收
ru_majflt: 頁(yè)失效
ru_nsignals: 收到的信號(hào)
ru_nvcsw: 主動(dòng)上下文切換
ru_nivcsw: 被動(dòng)上下文切換
ru_nswap: 交換區(qū)
ru_utime.tv_usec: 用戶(hù)態(tài)時(shí)間 (microseconds)
ru_utime.tv_sec: 用戶(hù)態(tài)時(shí)間(seconds)
ru_stime.tv_usec: 系統(tǒng)內(nèi)核時(shí)間 (microseconds)
ru_stime.tv_sec: 系統(tǒng)內(nèi)核時(shí)間 (seconds)
要看到你的腳本消耗了多少CPU,我們需要看看“用戶(hù)態(tài)的時(shí)間”和“系統(tǒng)內(nèi)核時(shí)間”的值。秒和微秒部分是分別提供的,您可以把微秒值除以100萬(wàn),并把它添加到秒的值后,可以得到有小數(shù)部分的秒數(shù)。
// sleep for 3 seconds (non-busy) sleep(3); $data = getrusage(); echo “User time: “. ($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000); echo “System time: “. ($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000); /* 輸出User time: 0.011552System time: 0sleep是不占用系統(tǒng)時(shí)間的,我們可以來(lái)看下面的一個(gè)例子: // loop 10 million times (busy) for($i=0;$i 10000000;$i++) { $data = getrusage(); echo “User time: “. ($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000); echo “System time: “. ($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000); /* 輸出User time: 1.424592System time: 0.004204這花了大約14秒的CPU時(shí)間,幾乎所有的都是用戶(hù)的時(shí)間,因?yàn)闆](méi)有系統(tǒng)調(diào)用。
系統(tǒng)時(shí)間是CPU花費(fèi)在系統(tǒng)調(diào)用上的上執(zhí)行內(nèi)核指令的時(shí)間。下面是一個(gè)例子:
$start = microtime(true); // keep calling microtime for about 3 seconds while(microtime(true) – $start 3) { $data = getrusage(); echo “User time: “. ($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000); echo “System time: “. ($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000); /* printsUser time: 1.088171System time: 1.675315我們可以看到上面這個(gè)例子更耗CPU。
5、獲取系統(tǒng)常量
PHP 提供非常有用的系統(tǒng)常量 可以讓你得到當(dāng)前的行號(hào) (__LINE__),文件 (__FILE__),目錄 (__DIR__),函數(shù)名 (__FUNCTION__),類(lèi)名(__CLASS__),方法名(__METHOD__) 和名字空間 (__NAMESPACE__),很像C語(yǔ)言。
我們可以以為這些東西主要是用于調(diào)試,當(dāng)也不一定,比如我們可以在include其它文件的時(shí)候使用 __FILE__ (當(dāng)然,你也可以在 PHP 5.3以后使用 __DIR__ ),下面是一個(gè)例子。
// this is relative to the loaded script's path // it may cause problems when running scripts from different directories require_once(‘config/database.php'); // this is always relative to this file's path // no matter where it was included from require_once(dirname(__FILE__) . ‘/config/database.php');下面是使用 __LINE__ 來(lái)輸出一些debug的信息,這樣有助于你調(diào)試程序:
// some code // … my_debug(“some debug message”, __LINE__); /* 輸出Line 4: some debug message// some more code // … my_debug(“another debug message”, __LINE__); /* 輸出Line 11: another debug messagefunction my_debug($msg, $line) { echo “Line $line: $msg/n”;6、生成唯一的id
很多朋友都利用md5()來(lái)生成唯一的編號(hào),但是md5()有幾個(gè)缺點(diǎn):1、無(wú)序,導(dǎo)致數(shù)據(jù)庫(kù)中排序性能下降。2、太長(zhǎng),需要更多的存儲(chǔ)空間。其實(shí)PHP中自帶一個(gè)函數(shù)來(lái)生成唯一的id,這個(gè)函數(shù)就是uniqid()。下面是用法:
// generate unique string echo uniqid(); /* 輸出4bd67c947233e// generate another unique string echo uniqid(); /* 輸出4bd67c9472340該算法是根據(jù)CPU時(shí)間戳來(lái)生成的,所以在相近的時(shí)間段內(nèi),id前幾位是一樣的,這也方便id的排序,如果你想更好的避免重復(fù),可以在id前加上前綴,如:
// 前綴 echo uniqid(‘foo_'); /* 輸出foo_4bd67d6cd8b8f// 有更多的熵 echo uniqid(”,true); /* 輸出4bd67d6cd8b926.12135106// 都有 echo uniqid(‘bar_',true); /* 輸出bar_4bd67da367b650.436846477、序列化
PHP序列化功能大家可能用的比較多,也比較常見(jiàn),當(dāng)你需要把數(shù)據(jù)存到數(shù)據(jù)庫(kù)或者文件中是,你可以利用PHP中的serialize() 和 unserialize()方法來(lái)實(shí)現(xiàn)序列化和反序列化,代碼如下:
// 一個(gè)復(fù)雜的數(shù)組 $myvar = array( ‘hello', array(1,'two'), ‘a(chǎn)pple' // 序列化 $string = serialize($myvar); echo $string; /* 輸出a:4:{i:0;s:5:”hello”;i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:”two”;}i:3;s:5:”apple”;}// 反序例化 $newvar = unserialize($string); print_r($newvar); /* 輸出Array[0] = hello[1] = 42[2] = Array[0] = 1[1] = two[3] = apple如何序列化成json格式呢,放心,php也已經(jīng)為你做好了,使用php 5.2以上版本的用戶(hù)可以使用json_encode() 和 json_decode() 函數(shù)來(lái)實(shí)現(xiàn)json格式的序列化,代碼如下:
// a complex array$myvar = array( ‘hello', array(1,'two'), ‘a(chǎn)pple' // convert to a string $string = json_encode($myvar); echo $string; /* prints["hello",42,[1,"two"],”apple”]// you can reproduce the original variable $newvar = json_decode($string); print_r($newvar); /* printsArray[0] = hello[1] = 42[2] = Array[0] = 1[1] = two[3] = apple8、字符串壓縮
當(dāng)我們說(shuō)到壓縮,我們可能會(huì)想到文件壓縮,其實(shí),字符串也是可以壓縮的。PHP提供了 gzcompress() 和gzuncompress() 函數(shù):
$string = “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut elit id mi ultricies adipiscing. Nulla facilisi. Praesent pulvinar, sapien vel feugiat vestibulum, nulla dui pretium orci, non ultricies elit lacus quis ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam pretium ullamcorper urna quis iaculis. Etiam ac massa sed turpis tempor luctus. Curabitur sed nibh eu elit mollis congue. Praesent ipsum diam, consectetur vitae ornare a, aliquam a nunc. In id magna pellentesque tellus posuere adipiscing. Sed non mi metus, at lacinia augue. Sed magna nisi, ornare in mollis in, mollis sed nunc. Etiam at justo in leo congue mollis. Nullam in neque eget metus hendrerit scelerisque eu non enim. Ut malesuada lacus eu nulla bibendum id euismod urna sodales. “; $compressed = gzcompress($string); echo “Original size: “. strlen($string).”/n”; /* 輸出原始大小Original size: 800echo “Compressed size: “. strlen($compressed).”/n”; /* 輸出壓縮后的大小Compressed size: 418// 解壓縮 $original = gzuncompress($compressed); 幾乎有50% 壓縮比率。同時(shí),你還可以使用 gzencode() 和 gzdecode() 函數(shù)來(lái)壓縮,只不用其用了不同的壓縮算法。
以上就是8個(gè)開(kāi)發(fā)必備的PHP功能,是不是都很實(shí)用呢?PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久伊人国产精品 | 国产99久久精品一区二区 | 2019中文字幕在线播放 | 午夜男人在线观看 | 久久久久成人免费 | 亚洲第一综合 | 天天干干| 超碰97最新| 国产精品99久久久久久久女警 | av电影网在线观看 | www成人在线观看 | 成人午夜网址 | 久久av免费| 国产精品成人一区 | 中文字幕一区二区三区四区 | 一本色道久久综合狠狠躁篇适合什么人看 | 日日摸夜夜骑 | 国内精品久久久久久久影视红豆 | 狠狠操视频网站 | 国内精品国产三级国产a久久 | 亚洲精品午夜国产va久久成人 | 亚欧美一区二区 | 成人黄色短视频在线观看 | 最新在线中文字幕 | 黄色片网站在线免费观看 | 91精彩视频 | 逼特逼视频在线观看 | 国产pron | 粉嫩av一区二区三区四区在线观看 | 水卜樱一区二区av | 88xx成人永久免费观看 | 日日摸夜夜骑 | 黄色片免费看网站 | 91av亚洲| 免费在线性爱视频 | 久久新网址 | 狠狠久久 | 欧美黄成人免费网站大全 | 久久精品视频7 | 久久精品在线免费观看 | 久久国产精品一区 |