Magic constants for file ingestion, function handling, recursion and iteration "divide and conquer", anonymous functions, arrays and array pointer contents

Source: Internet
Author: User

1. Note The problem of magic constants being introduced
a.php loading b.php "where b.php output magic constant __file__ output is the path of b.php"
Magic constants are compiled in order before the code in a is processed "can be understood as magic constants in the pre-introduction of the compilation process has been pre-compiled"
The magic constant equals the placeholder "instead of a variable in the ingest file"
"Notice how the magic constant is handled."
Understanding of the file loading process further "is that the file load statement is executed in the file, the loaded file is compiled and copied (so that the problem of loading to ensure resolution)"
"Also load file scope problem: The scope of the load file depends on the location of the load (example is to load a file in the function, the scope of the variable in the file is

function) "

"Think of Magic constants as a special case."

2.call_user_func_array ();//Implement the delivery of custom functions and parameters

3. Recursive invocation of functions!!! Focus
function body again call function itself!!!
"Recursion itself has no difficulty, and function can be called in any case, because function itself is a set of execution code, can be arbitrarily called, just the outer

function has not been normal return, that is, the function of the end of the layer need to return the keyword to describe. 】
Function F1 () {
F1 ();//The call is implemented here, but the condition needs to be judged
Scenarios that are suitable for recursive invocation: such as traversing a directory
The invocation of the case processing is consistent, that is, there is an end to the condition "because of this recursive invocation, can save the number of defined functions"
Actually recursive call in actual usage is relatively less used
"Just to decompose a complex thing until the event cannot be decomposed."

Example: by recursively implementing the Fibonacci sequence "the latter is the first two and"
function f ($n) {
First, the basic criteria are filtered
if ($n ==1) {
return 1;
}elseif ($n ==2) {
return 1;
Start using formulas for recursion
Return F ($n-1) + F ($n-2);
f ();//Initialize Call
This enables recursion

The focus of recursion:
(1) Recursion point: when recursion occurs
(2) The exit of recursion: When is the end of recursion?

In fact, the recursion itself does not have any difficulty, and the direct call function no difference, is to invoke the conditions to be used to determine the need to pay attention to
"Function call itself does not need too much worry, is simple to declare and invoke the problem, normal understanding can be"

4. Iteration
The realization of Fibonacci sequence by iteration
function f ($n) {
$before _1=1;
$before _2=1;//Initialize the first two values of $n
We also need to judge the conditions of the first two $n.
Switching between the first two items and iterating through the loop all the time, you can always get
for ($i =3; $i <= $n; $n + +) {
Iterate over the values of the first two items and add them to
Recursive usage scenarios are well avoided by iterating over the elements of the recursive process.

The iterative grammatical system is the cyclic structure "This is to note: The loop to achieve the effect of recursive such loop execution"
"So there's no conflict."
"And with iterations, it's more efficient."
Iteration and recursion belong to the divide-and-conquer algorithm "divide a complex problem into several small problems and then process"

5. Functions for handling functions
function handling "manual use"
Some of the more common processing functions use
Function_exists () "Returns a Boolean value"
Create_function () "Returns a function name" "To reorganize an existing method by this method"
MAGIC constant __function__ "gets the current function name within the function (for example, dynamically calling itself in a recursive call)"

How to create a function so far
Name of function+ function
function+ Anonymous functions
Creat_function Create a more flexible function

6. Anonymous functions
The implementation approach is achieved through the closure object.
Use syntax "Note"

Magic constants for file ingestion, function handling, recursion and iteration "divide and conquer", anonymous functions, arrays and array pointer contents

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