2006/02/21

learn:php语法学习,函数

tags:php,learn,语法,函数

函数
function foo ( $arg_1, $arg_2 , ..., $arg_n)
{
echo
"Example function.\n"
;
return
$retval
;
}

任何有效的 PHP 代码都有可能出现在函数内部,甚至包括其它函数和定义。
PHP 中的所有函数和类都具有全局域,可以在内部定义外部调用,反之亦然。
PHP 不支持函数重载,也不可能取消定义或者重定义已声明的函数。
函数名是非大小写敏感的,不过在调用函数的时候,通常使用其在定义时相同的形式。
在 PHP 中可以调用递归函数。但是要避免递归函数/方法调用超过 100-200 层,因为可能会破坏堆栈从而使当前脚本终止。
--tom pittlik 04-Feb-2006 11:53
function format_string( $string ,$functions )
{
$funcs = explode ("," , $functions
);

foreach (
$funcs as $func
)
{
if (
function_exists ($func)) $string = $func ($string
);
}

return
$string
;
}

echo
format_string (" <b> this is a test </b>", "strip_tags,strtoupper,trim"
);
// outputs "THIS IS A TEST"


--removeloop at removesuperinfinite dot com 03-Aug-2003 01:56
class A {
function
A
() { }

function
ech
() {
$a = func_get_args
();
for(
$t=0 ;$t<count ($a); $t
++ ) {
echo
$a [$t
];
}
}
}

$test = new A
();
$test-> ech( 0, 1,2 , 3,4, 5,6, 7, 8, 9
);


// output:
// 0123456789


--spy-j at rainbowtroopers dot com 21-Dec-2004 03:01
UNSET( $refArray ); //<-- fatal!!! so if not empty set $refArray='';
function bar ( & $refArray, ....) {

if (!EMPTY(
$refArray )) $refArray = ''
;

: : :

}
//end bar

通过参数列表可以传递信息到函数,即以逗号作为分隔符的表达式列表。
PHP 支持按值传递参数(默认), 通过引用传递 以及 默认参数。可变长度参数列表仅在 PHP 4 和后续版本中支持
如果想要函数的一个参数总是通过引用传递,可以在函数定义中该参数的前面预先加上符号 &:
function add_some_extra (& $string)
{
$string .= 'and something extra.'
;
}
$str = 'This is a string, '
;
add_some_extra ( $str
);
echo
$str ;
// outputs 'This is a string, and something extra.'
默认参数的值

function makecoffee ( $type = "cappuccino")
{
return
"Making a cup of $type.\n"
;
}
echo
makecoffee
();
echo
makecoffee("espresso"
);

output:
Making a cup of cappuccino.
Making a cup of espresso.
使用非标量类型作为默认参数

function makecoffee ( $types = array("cappuccino" ), $coffeeMaker = NULL)
{
$device = is_null($coffeeMaker ) ? "hands" : $coffeeMaker
;
return
"Making a cup of " .join( ", " , $types). " with $device.\n"
;
}

默认值必须是常量表达式,不是(例如)变量,类成员,或者函数调用。
请注意当使用默认参数时,任何默认参数必须放在任何非默认参数的右侧;否则,可能函数将不会按照预期的情况工作。
可变长度参数列表
PHP 4 及更高版本已经在用户自定义函数中支持可变长度参数列表。这个真的很简单,用 func_num_args() func_get_arg(),和 func_get_args() 函数。
无需特别的语法,参数列表仍然能够被明确无误的传递给函数并且正常运转。
public function __construct() {
echo func_num_args() . "<br>";
var_dump( func_get_args());
echo "<br>";
}


返回值

值通过使用可选的返回语句返回。任何类型都可以返回,其中包括列表和对象。这导致函数立即结束它的运行,并且将控制权传递回它被调用的行。更多信息见 return()
return $num * $num ;
返回一个数组以得到多个返回值
return array (0, 1 , 2 );
由函数返回一个引用
function &returns_reference ()
{
return
$someref
;
}
$newref =& returns_reference();

--nick at itomic.com 04-Aug-2003 12:56
function &testRet ()
{
return
NULL
;
}

if (
testRet () === NULL
)
{
echo
"NULL"
;
}


--rusty at socrates dot berkeley dot edu 17-Jul-2003 02:48
function myfunc($myvar) {
if ($myvar == "abc")
return(1);

if ($myvar == "xyz")
return(2);

return;
}

$abc = myfunc("def");

if (isset($abc))
echo("a-ok");
else
echo("oops");

变量函数
PHP 支持变量函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数,并且将尝试执行它。除了别的事情以外,这个可以被用于实现回调函数,函数表等等。
变量函数不能用于语言结构,例如 echo() print()unset()isset()empty()include() require() 以及类似的语句。需要使用自己的外壳函数来将这些结构用作变量函数。
class Foo
{
function
Variable
()
{
$name = 'Bar'
;
$this->$name ();
// This calls the Bar() method
}

function
Bar
()
{
echo
"This is Bar"
;
}
}

$foo = new Foo();
$funcname = "Variable"
;
$foo->$funcname();
// This calls $foo->Variable()

--Storm 03-May-2005 08:34
This can quite useful for a dynamic database class:

class db {

private
$host = 'localhost'
;
private
$user = 'username'
;
private
$pass = 'password'
;
private
$type = 'mysqli'
;

public
$lid = 0
;

// Connection function
function connect
() {
$connect = $this ->type.'_connect'
;

if (!
$this ->lid = $connect ($this->host , $this-> user, $this-> pass
)) {
die(
'Unable to connect.'
);
}
}
}
$db = new db
;
$db ->connect
();
内部(内置)函数

PHP 有很多标准的函数和结构。还有一些函数需要和特定地 PHP 扩展模块一起编译,否则在使用它们的时候就会得到一个致命的"未定义函数"错误。例如,要使用图像函数比如 imagecreatetruecolor(),需要在编译 PHP 的时候加上 GD 的支持。或者,要使用 mysql_connect() 函数,就需要在编译 PHP 的时候加上 MySQL 支持。有很多核心函数已包含在每个版本的 PHP 中如字符串变量函数。调用 phpinfo() 或者 get_loaded_extensions() 可以得知 PHP 加载了那些扩展库。同时还应该注意,很多扩展库默认就是有效的。PHP 手册按照不同的扩展库组织了它们的文档。请参阅 配置安装以及各自的扩展库章节以获取有关如何设置 PHP 的信息。

手册中如何阅读函数原型讲解了如何阅读和理解一个函数的原型。确认一个函数将返回什么,或者函数是否直接作用于传递的参数是很重要的。例如,str_replace() 函数将返回修改过的字符串,而 usort() 却直接作用于传递的参数变量本身。手册中,每一个函数的页面中都有关于函数参数、行为改变、成功与否的返回值以及使用条件等信息。了解这些重要的(常常是细微的)差别是编写正确的 PHP 代码的关键。

参见 function_exists()函数参考 get_extension_funcs()dl()




--
[:p] --飞扬.轻狂 [fallseir.lee]
http://fallseir.livejournal.com/
http://feed.feedsky.com/fallseir

没有评论: