PHP的真正威力源自于它的函数,但有些PHP函数并没有得到充分的利用,也并不是所有人都会从头到尾一页一页地阅读手册和函数参考,这里将向您介绍这些实用的函数和功能。
1、任意参数数目的函数
你可能已经知道,PHP允许定义可选参数的函数。但也有完全允许任意数目的函数参数的方法。以下是可选参数的例子:
以下为引用的内容:
//functionwith2optionalarguments
functionfoo($arg1=”,$arg2=”){
echo“arg1:$arg1
”;
echo“arg2:$arg2
”;
}
foo(‘hello’,world’);
/*prints:
arg1:hello
arg2:world
*/
foo();
/*prints:
arg1:
20. arg2:
*/
现在让我们看看如何建立能够接受任何参数数目的函数。这一次需要使用func_get_args()函数:
以下为引用的内容:
//yes,theargumentlistcanbeempty
functionfoo(){
//returnsanarrayofallpassedarguments
$args=func_get_args();
foreach($argsas$k=>$v){
echo“arg”.($k 1).”:$v
”;
}
}
foo();
/*printsnothing*/
foo(‘hello’);
/*prints
arg1:hello
*/
foo(‘hello’,‘world’,‘again’);
/*prints
arg1:hello
arg2:world
arg3:again
*/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
