如果我编写一个类似的函数
function exd ($user = null) {
return $user;
}
如果我调用像这样没有变量的函数
exd();
该值将为null
?
But if I give the function a variable does the variable that I give become `$user`?
exd('account1');
谢谢,在文档中找不到,所以我想在这里问一下?!
调用exd()
返回null,因为$user
被设置为null作为默认值。如果您使用exd('account1')
,它将返回account1
。因为内部的$user
变量,所以函数得到一个值account1
。在函数之外,您将无法获得$user
的值。
这里有一个有用的示例。
<?PHP
function exd($user = NULL){
return $user;
}
//Does nothing outside the function
exd();
//Does nothing outside the function
exd("Joe");
//$foo now = NULL
$foo = exd();
//$foo now = string Joe
$foo = exd("Joe");
?>
如果您确实想修改$user,这里有一个可能有用的辅助功能
<?PHP
$user = "Jill";
function exd(&$user, $newName=NULL){
$user = $newName;
}
//Outputs Jill
echo $user;
//Outputs Joe
exd($user, "Joe");
echo $user
//Outputs null
exd($user);
echo $user;
?>
如果用户是比字符串更复杂的东西,这将更有用。