Home > Backend Development > PHP Tutorial > Notes on using PHP functions Notes on using solid state drives Notes on using ID cards etc. Notes on using

Notes on using PHP functions Notes on using solid state drives Notes on using ID cards etc. Notes on using

WBOY
Release: 2016-07-29 08:55:18
Original
1428 people have browsed it

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.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template