Note:
1. PHP function parameters, when using default parameters, any default parameters must be placed to the right of any non-default parameters, otherwise the function will not work as expected.2. PHP cannot return multiple values, but you can achieve the same effect by returning an array
function getArr(){
Return array(1,2,3);
}
list( $a,$b,$c)=getArr();
3. To return a reference from a function, you must use the operator &
global $arr;
$ both when declaring the function and when assigning the return value to a variable. arr=array(3);
function & return_reference(){
global $arr;
print_r($arr);
return $arr;
}
$newref =& return_reference();
$newref[0]=4;
$newref=& return_reference();
4. Anonymous function
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;
$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);The above introduces the points to note when using PHP functions, including usage notes and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.