注意:
1、php函數參數,當使用預設參數時,任何預設參數必須放在任何非預設參數的右側,否則函數將不按照預期工作。
2、php不能返回多個值,但可以通過返回一個數組達到相同的效果
function getArr(){
return array(1,2,3);
}
list($a,$b,$c)=getArr();
3、從函數返回一個引用,必須在函式宣告和指派返回值給一個變數時都使用運算子&
global $arr;
$arr=array(3);
function & return_reference(){
global $arr;
print_r($arr);
return $arr;
}
$newref=& return_reference();
$newref[0]=4;
$newref=& return_reference();
4、匿名函數
class Cart{
const PRICE_BUTTER=1.00;
const PRICE_MILK=3.00;
const PRICE_EGG=6.95;
protected $products=array();
public function getQuantity($product){
return isset($this->products[$product])?$this->products[$product]:FALSE;
}
public function getTotal($tax){
$total=0.00;
$callback=function($quantity,$product) use ($tax,&$total){
$pricePerItem=constant(__CLASS__."::PRICE_".strtoupper($product));
$total +=($pricePerItem*$quantity)*($tax+1.0);
}
array_walk($this->products,$callback);
return round($total,2);
}
}
$mycart=new Cart;
$mycart->add('butter',1);
$mycart->add('milk',3);
$mycart->add('eggs',6);
echo $mycart->getTotal(0.05);
以上就介紹了PHP 函數使用注意點,包括了使用注意,php方面的內容,希望對PHP教程有興趣的朋友有所協助。