PHP Preliminary (middle)

Source: Internet
Author: User

I. Loading of documents

File loading: Loading the code of the target file into its current location, sometimes called Process control.

The purpose of the file is to achieve a hierarchical design of the site, because different pages may have many of the same area (the same code), these same code only need to write once, when necessary to load in, improve code reuse, improve efficiency.

The loaded file, whatever the type, actually loads the code inside the file, regardless of what type the file is. The loaded code, in addition to the PHP code can be executed, other scripts can also be executed.

Include, require, include-once, require-once

Include ' destination file address '//relative to the current file

Include-once will be checked at load time, if the file is loaded, it will not be loaded

The include and require depend on the load file, the include load fails, generates an warning level error, the subsequent code continues execution, require load fails, error, and subsequent code is no longer executed.

Loading principle:

1, when the implementation of the include, the first exit PHP mode, into the HTML mode

2. Load the source code of the target file

3. Compile and Execute

4. Re-enter PHP mode

Two, load path

Relative path:.. /.//default is the root directory of the site is not written (indicates the default path, can be set in PHP config file php.ini include_path)

Absolute path: The code in the target file can be introduced regardless of where the current file is opened, and the disadvantage is that the portability is poor.

To improve the portability of files for absolute paths, we can:

1, first define with the directory constants, Magic constants

Since the directory delimiter under the Linux operating system only supports forward slashes/, in order to be compatible, the backslash in the above path must be changed to a forward slash, and this time, a system function str_replace is needed to replace it:

Define ("Root_dir", str_replace (' \ \ ', '/', __dir__). ’/’);

2. Define other Directory constants

Define ("Inc_dir", Root_dir. ' includes/');

3. Loading files

Include Inc_dir. ' filename ';

iii. Termination of script execution

In general, the function terminates with exit, the function external die

There are many more tests for code:

1, check the error, gradually troubleshooting

2, look at the output of the intermediate code, early termination of script execution

3, business logic needs, for example, wrong password. Connection failure, etc.

Script deferred execution: Sleep (number of seconds)

Sleep is most commonly used in the test phase of code, using sleep to view the current intermediate code and perform the effect

The number of seconds after sleep is typically no more than 30 seconds, because PHP's default script period is 30 seconds! Configuring Max_execution_time = 30 in php.ini

Iv. function Preliminary

The function must be declared before it is called, but it can be called before the code is written, because the script is precompiled once before execution, and the pre-compiled function's definition code is placed in the user code area of the memory.

The function name follows the basic identifier naming rules, but the function names are not case-sensitive and are generally represented by the small hump Method! (Note: The customary later class name with the large hump method, because the class name is also not case-sensitive).

Formal parameter parameter, actual parameter argument

The pass of a parameter is passed by default, or it can be passed, that is, precede the formal parameter with the address operator &, which is the address that the argument is passed to the formal parameter, and the argument must be a variable, and the variable has a reference pass.

You can assign a value to a parameter to a default value

Indeterminate parameters

Because the number of arguments is indeterminate, one does not define

Func_get_args () receives all the arguments passed, obtains an indexed array, accesses it by subscript, and then operations on it

Count () Gets the number of array elements

Func_get_arg () Gets the value of a parameter with the subscript of the parameter in parentheses

Func_num_args () Gets the number of arguments

V. Scope of variables

Super global variables, global variables, local variables

1, the global and local scopes in PHP are completely non-overlapping, that is, they are not accessible to each other!

2, each function also has its own local scope, the variables between functions can not access each other!

Super Global variables:

1. Each user defines a global variable that automatically adds an element to the array of hyper-global variables

2, through the subscript of the Super global variable, you can access its value anywhere in the $globals[' key ']= $value, conversely, increase the $globals element, you can also create a super global variable.

At this point, the Super global variable and the global variable is the "die" relationship! Delete, modify is synchronized!

Global

Copies the address of the global variable, generating a new local variable with the same name (reference pass)

Deletes the global declared variable without affecting the GLOBALS variable.

Use Hyper global variable $globals or global, as appropriate, use $globals ("die") to avoid impact on business logic

Static local variable static

The so-called static local variable, is that the variable does not disappear with the end of the function execution, but, as long as the function is still (in memory), the variable exists!

Maximum feature: Each time the function is called, the static local variables are saved without disappearing (you can use them next time you call the function)!

In a looping statement, static local variable statics initialization executes only once and does not reinitialize as the loop progresses!

Vi. recursive invocation and iterative thinking

(Will be taken out in detail in a separate analysis)

Syntactically, the recursive invocation of the so-called function is that the function automatically calls itself when it executes. One of the most typical: Traverse directories!

The recursion of a function has two elements:

1, recursive exit: When do you stop calling yourself

2, recursion point: When you start calling yourself

The iteration is to solve the problem from the go to later, embodied in the Code, is actually a step of repeated execution, that is, the loop structure.

PHP Preliminary (middle)

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.