咱們在.NET或者JAVA編程中,通常函數參數個數都是固定的,可是PHP容許你使用任意個數的參數。下面這個示例向你展現了PHP函數的默認參數:php
// 兩個默認參數的函數算法
function foo($arg1 = 」, $arg2 = 」) {數據庫
echo 「arg1: $arg1\n」;編程
echo 「arg2: $arg2\n」;json
}windows
foo(‘hello’,’world’);api
/* 輸出:數組
arg1: helloapp
arg2: world函數
*/
foo();
/* 輸出:
arg1:
arg2:
*/
下面這個示例是PHP的不定參數用法,其使用到了 func_get_args()方法:
// 是的,形參列表爲空
function foo() {
// 取得全部的傳入參數的數組
$args = func_get_args();
foreach ($args as $k => $v) {
echo 「arg」.($k+1).」: $v\n」;
}
}
foo();
/* 什麼也不會輸出 */
foo(‘hello’);
/* 輸出
arg1: hello
*/
foo(‘hello’, ‘world’, ‘again’);
/* 輸出
arg1: hello
arg2: world
arg3: again
*/
大部分PHP函數的函數名從字面上均可以理解其用途,可是當你看到 glob() 的時候,你也許並不知道這是用來作什麼的,其實glob()和scandir() 同樣,能夠用來查找文件,請看下面的用法:
// 取得全部的後綴爲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
)
*/
若是你想獲得絕對路徑,你能夠調用 realpath() 函數:
$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.jpg
)
*/
PHP的內存回收機制已經很是強大,你也可使用PHP腳本獲取當前內存的使用狀況,調用memory_get_usage() 函數獲取當期內存使用狀況,調用memory_get_peak_usage() 函數獲取內存使用的峯值。參考代碼以下:
echo 「Initial: 「.memory_get_usage().」 bytes \n」;
/* 輸出
Initial: 361400 bytes
*/
// 使用內存
for ($i = 0; $i < 100000; $i++) {
$array []= md5($i);
}
// 刪除一半的內存
for ($i = 0; $i < 100000; $i++) {
unset($array[$i]);
}
echo 「Final: 「.memory_get_usage().」 bytes \n」;
/* prints
Final: 885912 bytes
*/
echo 「Peak: 「.memory_get_peak_usage().」 bytes \n」;
/* 輸出峯值
Peak: 13687072 bytes
*/
獲取了內存使用狀況,也可使用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
)
*/
這個結構看上出很晦澀,除非你對CPU很瞭解。下面一些解釋:
要看到你的腳本消耗了多少CPU,咱們須要看看「用戶態的時間」和「系統內核時間」的值。秒和微秒部分是分別提供的,您能夠把微秒值除以100萬,並把它添加到秒的值後,能夠獲得有小數部分的秒數。
// 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.011552
System time: 0
*/
sleep是不佔用系統時間的,咱們能夠來看下面的一個例子:
// 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.424592
System time: 0.004204
*/
這花了大約14秒的CPU時間,幾乎全部的都是用戶的時間,由於沒有系統調用。
系統時間是CPU花費在系統調用上的上執行內核指令的時間。下面是一個例子:
$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);
/* prints
User time: 1.088171
System time: 1.675315
*/
咱們能夠看到上面這個例子更耗CPU。
PHP 提供很是有用的系統常量 可讓你獲得當前的行號 (__LINE__),文件 (__FILE__),目錄 (__DIR__),函數名 (__FUNCTION__),類名(__CLASS__),方法名(__METHOD__) 和名字空間 (__NAMESPACE__),很像C語言。
咱們能夠覺得這些東西主要是用於調試,當也不必定,好比咱們能夠在include其它文件的時候使用?__FILE__ (固然,你也能夠在 PHP 5.3之後使用 __DIR__ ),下面是一個例子。
// 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__ 來輸出一些debug的信息,這樣有助於你調試程序:
// 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 message
*/
function my_debug($msg, $line) {
echo 「Line $line: $msg\n」;
}
六、生成惟一的id
不少朋友都利用md5()來生成惟一的編號,可是md5()有幾個缺點:一、無序,致使數據庫中排序性能降低。二、太長,須要更多的存儲空間。其實PHP中自帶一個函數來生成惟一的id,這個函數就是uniqid()。下面是用法:
// generate unique string
echo uniqid();
/* 輸出
4bd67c947233e
*/
// generate another unique string
echo uniqid();
/* 輸出
4bd67c9472340
*/
該算法是根據CPU時間戳來生成的,因此在相近的時間段內,id前幾位是同樣的,這也方便id的排序,若是你想更好的避免重複,能夠在id前加上前綴,如:
// 前綴
echo uniqid(‘foo_’);
/* 輸出
foo_4bd67d6cd8b8f
*/
// 有更多的熵
echo uniqid(」,true);
/* 輸出
4bd67d6cd8b926.12135106
*/
// 都有
echo uniqid(‘bar_’,true);
/* 輸出
bar_4bd67da367b650.43684647
*/
七、序列化
PHP序列化功能你們可能用的比較多,也比較常見,當你須要把數據存到數據庫或者文件中是,你能夠利用PHP中的serialize() 和 unserialize()方法來實現序列化和反序列化,代碼以下:
// 一個複雜的數組
$myvar = array(
‘hello’,
42,
array(1,’two’),
‘apple’
);
// 序列化
$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也已經爲你作好了,使用php 5.2以上版本的用戶可使用json_encode() 和 json_decode() 函數來實現json格式的序列化,代碼以下:
// a complex array
$myvar = array(
‘hello’,
42,
array(1,’two’),
‘apple’
);
// 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);
/* prints
Array
(
[0] => hello
[1] => 42
[2] => Array
(
[0] => 1
[1] => two
)
[3] => apple
)
*/
八、字符串壓縮
當咱們說到壓縮,咱們可能會想到文件壓縮,其實,字符串也是能夠壓縮的。PHP提供了 gzcompress() 和gzuncompress() 函數:
$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: 800
*/
echo 「Compressed size: 「. strlen($compressed).」\n」;
/* 輸出壓縮後的大小
Compressed size: 418
*/
// 解壓縮
$original = gzuncompress($compressed);
幾乎有50% 壓縮比率。同時,你還可使用 gzencode() 和 gzdecode() 函數來壓縮,只不用其用了不一樣的壓縮算法。
以上就是8個開發必備的PHP功能,是否是都很實用呢?