注意:
1、php函数参数,当使用默认参数时,任何默认参数必须放在任何非默认参数的右侧,否则函数将不按照预期工作。2、php不能返回多个值,但可以通过返回一个数组达到相同的效果
function getArr(){
return array(1,2,3);
}
立即学习“PHP免费学习笔记(深入)”;
list($a,$b,$c)=getArr();
3、从函数返回一个引用,必须在函数声明和指派返回值给一个变量时都使用运算符&
global $arr;
$arr=array(3);
function & return_reference(){
global $arr;
print_r($arr);
return $arr;
}
立即学习“PHP免费学习笔记(深入)”;
$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);
}
}
立即学习“PHP免费学习笔记(深入)”;
$mycart=new Cart;
$mycart->add('butter',1);
$mycart->add('milk',3);
$mycart->add('eggs',6);
echo $mycart->getTotal(0.05);
以上就介绍了PHP 函数使用注意点,包括了使用注意,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号