PHP functions use note points SSD usage precautions ID Use precautions etc use note things

Source: Internet
Author: User
Attention:

1, php function parameters, when using the default parameters, any default parameters must be placed on the right side of any non-default parameters, otherwise the function will not work as expected.

2. PHP cannot return multiple values, but can achieve the same effect by returning an array

function Getarr () {

return Array (+/-);

}

List ($a, $b, $c) =getarr ();

3, return a reference from the function, you must use the operator when declaring and assigning return values to a variable &

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. 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;

}

Public Function Gettotal ($tax) {

$total =0.00;

$callback =function ($quantity, $product) use ($tax,& $total) {

$pricePerItem =constant (__class__. "::P rice_". 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 use of PHP function note points, including the use of attention, PHP content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.