Function-PHP manual notes

Source: Internet
Author: User
: This article mainly introduces the notes in the function-PHP Manual. if you are interested in the PHP Tutorial, please refer to them. User-defined functions

The function does not need to be defined before calling unless it is conditional.

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

Specifically, the maximum depth of the PHP recursive stack on my computer is 100.

Function parameters

PHP supports passing parameters by value (default), passing parameters by reference, and default parameters. The variable length parameter list is also supported. PHP allows the use of arrays and special typesNULLAs the default parameter. If you want to allow the function to modify the parameter value, you must pass the parameter through reference.

Note! When using default parameters, any default parameters must be placed on the right of any non-default parameters; otherwise, the function will not work as expected.

PHP supports variable parameter lists in user-defined functions. In PHP 5.6 and later versions...Syntax implementation, which can be written in this way, and cannot be tested because there is no suitable environment.


  

Of course, you can also use a variable number of parameter lists in lower versions such as PHP 5.5. Functionfunc_num_args(),func_get_arg(),gunc_get_args()Obtain the number of parameters, the content of a parameter, and the parameter array.

 

Return value

The function cannot return multiple values, but you can return an array to obtain similar results.

 

Returns a reference from a function. the reference operator must be used when the function declaration and the return value are assigned to a variable.&.

 

Variable functions

PHP supports variable functions. that is to say, if a variable name has parentheses, PHP searches for a function with the same name as the variable value and tries to execute it. Variable functions can be used to implement callback functions and function tables, although I have never used them.

When a static method is called, the function call takes precedence over the static attribute.

Internal functions

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

If the parameter type passed to the function is different from the actual type, the return value of the function is uncertain. In this case, the function usually returnsNULL. But this is just a convention, not necessarily.

Anonymous functions

An Anonymous function (Anonymous functions), also called a closure function (closures), allows you to temporarily create a function without a specified name. The value that is most often used as the callback function parameter.

The first example program in the manual is related to a regular expression, which is not well understood. The second example program is an anonymous function variable value assignment. Note: add points at the end of the definition statement of the anonymous function variable.

 

In the third example program, I am sorry that I did not understand the variables inherited from the parent scope. The fourth example program, about Closures and scope, and the actual combination, good, take a closer look.

This is a basic shopping cart class that allows you to add a certain number of items and calculate the total price. A closure is used as the callback function.

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

Note! Do not drop the semicolon at the end of an anonymous function statement!

(Full text)

The above describes the notes in the function-PHP manual, including the relevant content, and hopes to help those who are interested in the PHP Tutorial.

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.