PHP 函數使用注意點 固態硬碟使用注意事項 身份證使用注意事項 etc使用注意事

來源:互聯網
上載者:User
注意:

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教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.