即使使用 PHP 多年,也会偶然发现一些未曾了解的函数和功能。其中有些是非常有用的,但没有得到充分利用。并不是所有人都会从头到尾一页一页地阅读手册和函数参考!
1、任意参数数目的函数
你可能已经知道,PHP 允许定义可选参数的函数。但也有完全允许任意数目的函数参数的方法。以下是可选参数的例子:
// function with 2 optional arguments
function foo($arg1 = ”, $arg2 = ”) {
echo "arg1: $arg1";
echo "arg2: $arg2";
}
foo(‘hello’,’world’);
/* prints:
arg1: hello
arg2: world
*/
foo();
/* prints:
arg1:
arg2:
*/
现在让我们看看如何建立能够接受任何参数数目的函数。这一次需要使用 func_get_args() 函数:
// yes, the argument list can be empty
function foo() {
// returns an array of all passed arguments
$args = func_get_args();
foreach ($args as $k => $v) {
echo "arg".($k+1).": $v";
}
}
foo();
/* prints nothing */
foo(‘hello’);
/* prints
arg1: hello
*/
foo(‘hello’, ‘world’, ‘again’);
/* prints
arg1: hello
arg2: world
arg3: again
*/
2、使用 Glob() 查找文件
许多 PHP 函数具有长描述性的名称。然而可能会很难说出 glob() 函数能做的事情,除非你已经通过多次使用并熟悉了它。可以把它看作是比 scandir() 函数更强大的版本,可以按照某种模式搜索文件。
// get all php files
$files = glob(‘*.php’);
print_r($files);
/* output looks like:
Array
(
[0] => phptest.php
[1] => pi.php
[2] => post_output.php
[3] => test.php
)
*/
你可以像这样获得多个文件:
// get all php files AND txt files
$files = glob(‘*.{php,txt}’, GLOB_BRACE);
print_r($files);
/* output looks like:
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);
/* output looks like:
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:wampwwwimagesapple.jpg
[1] => C:wampwwwimagesart.jpg
)
*/
波比源码 » 9个必须知道的实用PHP函数和功能
buy levaquin sale purchase levaquin for sale
dutasteride order online celecoxib 100mg pills zofran 8mg pills
buy aldactone 100mg spironolactone 25mg cheap order diflucan 200mg for sale
order ampicillin 500mg for sale bactrim 960mg sale buy erythromycin 500mg sale
buy fildena 50mg bimatoprost cheap purchase robaxin online cheap
order sildenafil pill buy suhagra online cheap estrace 1mg sale
lamotrigine 50mg without prescription prazosin order tretinoin gel without prescription
tadalafil over the counter generic avana 100mg order voltaren 100mg for sale
tadalafil pills ed medications list viagra 100mg generic
buy arimidex pill viagra directions for use viagra tablets
cialis generique acheter 100mg du sildenafil viagra 25mg gГ©nГ©rique
prednisone over the counter cialis without rx buy sildenafil 100mg generic
cialis fГјr frauen cialis 20mg kaufen ohne rezept sildenafil 200mg kaufen generika
purchase accutane online stromectol 6mg for sale ivermectin 8000 mcg
buy provigil 100mg generic diamox 250mg over the counter order acetazolamide 250mg online
doxycycline 200mg price doxycycline 200mg canada order lasix 40mg generic
oral catapres tiotropium bromide price order tiotropium bromide 9 mcg generic
buy buspirone 5mg generic buspirone 10mg brand buy oxybutynin 5mg
order hytrin 5mg generic actos oral sulfasalazine 500mg brand
buy fosamax for sale buy famotidine online cost pepcid 20mg
tacrolimus order online purchase ropinirole pills ursodiol 150mg tablet
imuran 100mcg without prescription viagra for men over 50 cost sildenafil 50mg
medroxyprogesterone 10mg brand praziquantel 600 mg ca cyproheptadine 4 mg drug
provigil ca modafinil 100mg for sale generic ivermectin for humans
order luvox 100mg for sale ketoconazole 200 mg usa order glucotrol sale
isotretinoin over the counter buy isotretinoin 10mg without prescription buy deltasone
buy nootropil 800mg nootropil 800 mg pills sildenafil women
furosemide 100mg us purchase furosemide online cheap hydroxychloroquine buy online
itraconazole 100 mg for sale itraconazole 100 mg generic tinidazole 300mg oral
buy glycomet 1000mg generic purchase tadalafil pill tadalafil 5 mg tablet
clozapine 50mg generic brand dexamethasone 0,0,5 mg order dexamethasone pill
buy viagra 50mg pills purchase sildenafil online order lisinopril for sale
order omeprazole 20mg generic online slot games casino games online
brand lopressor 100mg cheap vardenafil order levitra 20mg generic
speechwriters best casino games play for real online casino games
professional research paper writers online thesis writing order viagra sale
order clomid 50mg without prescription online gambling free casino games no registration no download
order cialis pills cialis 5mg pills sildenafil price
real cialis fast shipping propranolol brand order clopidogrel 150mg
buy methotrexate 2.5mg pill reglan 20mg generic purchase reglan online
cheap baclofen 10mg toradol 10mg pills cost ketorolac
buy gloperba pill online casino games for real money quick hits free online slots
ranitidine 300mg over the counter celecoxib 200mg ca celebrex 100mg pill
red dog casino poker game that roulette
tadalafil 10mg tablet order tadalafil for sale buy cipro 500mg for sale
order metronidazole 200mg pills purchase metronidazole without prescription oral bactrim 960mg
cheap keflex 125mg cheap erythromycin 500mg buy erythromycin without prescription
rx pharmacy online viagra generic cialis online order cialis online cheap
order cefuroxime 250mg online cheap methocarbamol 500mg ca methocarbamol tablet
trazodone 50mg generic desyrel pill cost aurogra 50mg
help with papers help writing papers buy ivermectin 3 mg for humans
sildenafil for men over 50 purchase sildalis buy lamictal sale
buy prednisone 40mg isotretinoin 40mg usa amoxicillin 250mg pill
purchase azithromycin for sale prednisolone 40mg us order generic gabapentin
casino online games provigil 100mg pill order provigil sale
lasix online buy buy hydroxychloroquine 200mg sale buy plaquenil 400mg without prescription