Functions - PHP Manual Notes

WBOY
Release: 2016-08-08 09:29:19
Original
840 people have browsed it

User-defined functions

Function does not need to be defined before calling unless it is conditionally defined.

All functions and classes in PHP have global scope. PHP does not support function overloading, and it is not possible to undefine or redefine declared functions.

I tried it specifically. The maximum depth of the PHP recursion stack on my computer is 100.

Function parameters

PHP supports passing parameters by value (default), passing parameters by reference, and default parameters. Variable length parameter lists are also supported. PHP allows using arrays and special types NULL as default parameters. If you want to allow a function to modify parameter values, you must pass parameters by reference.

Attention! 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.

PHP supports variable number of parameter lists in user-defined functions. In PHP 5.6 and above, there is ... syntax implementation, which can be written like this, but it cannot be tested because there is no suitable environment.

<code><?php 
function sum(...$numbers) {
	$acc = 0;
	foreach($numbers as $n) {
		$acc += $n;
	}
	return $acc;
}
echo sum(1, 2, 3, 4);</code>
Copy after login

Of course, variable number of parameter lists can also be used in lower versions such as PHP 5.5. Obtain the number of parameters, the content of a certain parameter, and the parameter array through the functions func_num_args(), func_get_arg(), gunc_get_args().

<code><?php 
function sum() {
	$acc = 0;
	foreach(func_get_args() as $n) {
		$acc += $n;
	}
	return $acc;
}
echo sum(1, 2, 3, 4);</code>
Copy after login

Return value

Functions cannot return multiple values, but you can get a similar effect by returning an array.

<code><?php 
function small_numbers() {
	return array(0, 1, 2);
}
list($zero, $one, $two) = small_numbers();
echo "$zero  $one  $two";</code>
Copy after login

To return a reference from a function, you must use the reference operators & both when declaring the function and when assigning the return value to a variable.

<code><?php 
function & return_reference() {
	return $someref;
}
$newref = & return_reference();</code>
Copy after login

Variable function

PHP supports variadic functions, that is, if there are parentheses after a variable name, PHP will look for a function with the same name as the variable's value and try to execute it. Variable functions can be used to implement callback functions, function tables, etc., although I have never used these.

When calling static methods, function calls take precedence over static properties.

Internal functions

PHP has many standard functions and structures, and some functions need to be compiled with specific PHP extension modules.

If the parameter type passed to the function is inconsistent with the actual type, the return value of the function is undefined. In this case, usually the function returns NULL. But this is just a convention and doesn't have to be the case.

Anonymous function

Anonymous functions, also called closures, allow you to temporarily create a function without a specified name. The value most commonly used as a callback function argument.

The first example program in the manual is related to regular expressions, which I don’t know much about. The second example program is an anonymous function variable assignment. Note that a semicolon should be added at the end of the definition statement of the anonymous function variable.

<code><?php 
$greet = function($name) {
	echo "Hello $name  ";
};
$greet("world");
$greet('PHP');</code>
Copy after login

The third example program, about inheriting variables from the parent scope, I don’t understand, which is a pity. The fourth sample program, about Closures and scope, is combined with actual practice. It’s good, take a closer look.

This is a basic shopping cart class that implements the function of adding a certain number of products and calculating the total price. A closure is used as a callback function.

<code><?php 
class Cart {
	const PRICE_BUTTER = 1.00;
	const PRICE_MILK = 3.00;
	const PRICE_EGGS = 6.95;

	protected $products = array();

	public function add($product, $quantity) {
		$this->products[$product] = $quantity;
	}
	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);</code>
Copy after login

Attention! Be sure not to leave the semicolon at the end of the anonymous function statement!

(Full text ends)

The above introduces the functions - PHP manual notes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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