<?
//方法一:
functionfoo(&$bar){
$bar.="andsomethingextra";
}
$str="ThisisaString,";
foo($str);
echo$str;//output:ThisisaString,andsomethingextra
echo"<br>";
//方法二:
functionfoo1($bar){
$bar.="andsomethingextra";
}
$str="ThisisaString,";
foo1($str);
echo$str;//output:ThisisaString,
echo"<br>";
foo1(&$str);
echo$str;//output:ThisisaString,andsomethingextra
?>
8、函数默认值。PHP中函数支持设定默认值,与C++风格相同。
<?
functionmakecoffee($type="coffee"){
echo"makingacupof$type.\n";
}
echomakecoffee();//"makingacupofcoffee"
echomakecoffee("espresso");//"makingacupofespresso"
/*
注意:当使用参数默认值时所有有默认值的参数应该在无默认值的参数的后边定义。否则,程序将不会按照所想的工作。
*/
functiontest($type="test",$ff){//错误示例
return$type.$ff;
}
9、PHP的几个特殊符号意义。
$变量
&变量的地址(加在变量前)
@不显示错误信息(加在变量前)
->类的方法或者属性
=>数组的元素值
?:三元运算子
10,include()语句与require()语句
如果要根据条件或循环包含文件,需要使用include()。
require()语句只是被简单的包含一次,任何的条件语句或循环等对其无效。
由于include()是一个特殊的语句结构,因此若语句在一个语句块中,则必须把他包含在一个语句块中。
<?
//下面为错误语句
if($condition)
include($file);
else
include($other);
//下面为正确语句
if($condition){
include($file);
}else
{
include($other);
}
?>