/* "goto" 5.3+ version */
Used to jump to a specified location in a program
The target location can be tagged with the target name plus a colon.
There are certain restrictions on Goto in PHP and can only jump in the same file and scope.
This means that you cannot jump out of a function or class method, or you can skip into another function.
You can't jump into any loop or switch structure either.
A common use is to jump out of a loop or switch, instead of a multi-layered break.
You can jump out of the loop (switch), but not from the outside. The function or class method is not available outward.
Goto A;
Echo ' Foo ';
A:
Echo ' Bar ';
/* "File load" */
Require/include/require_once/include_once
File loading is simply loading the code within the target file and executing it, regardless of the type of file being loaded
The file is loaded into the execution phase, and the code of the file is loaded when the statement is executed to require,
Compile and execute, and then go back to the statement location such as require to continue executing the following statement
Note
At the beginning of loading, exit PHP mode first;
Then load the target file code and execute the code;
At the end, enter PHP mode again.
Require: processing failed, resulting in E_compile_error error, script aborted.
Include: processing failed, resulting in e_warning error, script continues execution.
#不建议使用require_once/include_once
/* "Relative path" */
Which script the current browser is requesting, and which script the current location belongs to.
./file and file both represent file files in the current directory
File case (nested load file):
If the current directory does not find the file, continue looking in the directory where the code files are located.
If the file is found in the current directory, it will no longer be found in the directory where the code file is located and will not be loaded again.
__dir__ the directory where the script files are located
__file__ script file path
Include_path load File Find directory
Set_include_path () Set include_path, can be multiple, use string as argument
The path set by this function is valid only for that current file
This setting is valid only for lookup not directly written file path
Setting a new include_path will overwrite the original
Get_include_path () Gets the current include_path settings, no parameters
Path delimiter, semicolon under Windows, colon under Linux
Use predefined constant path_separator to get the current delimiter
If you write the file name directly:
1. The include_path is set
2. Current directory
3. Directory of files in which the code resides
If the file name is preceded by a path, it is looked up directly from the path, include_path directly ignored
/* "Return" */
Return with require, returns the contents of the file, and the return is written in the loaded file
Return can terminate execution of the script, as a normal script statement
return returns the corresponding value of the function
/* "Terminate and Defer script execution" */
Die/exit termination
Return is the execution of the terminating script
Die and exit immediately terminate script execution
Die ("To this End"); The string within the function can be output
Sleep () Delay (unit: seconds)
Default up to 30 seconds delay, PHP configuration can be modified Max_execution_time
Example: Sleep (12);
Usleep () delay execution with the specified number of microseconds
Time_sleep_until make the script sleep until the specified time
/* "function" */
1. The function declaration is at compile time, therefore the definition is called again, the definition and the invocation has no succession relationship!
2. The file is just the carrier of the code, the program is executed in memory!
3. If the definition of a function is within a file that needs to be loaded, you need to load the file first, otherwise the call will be wrong!
4. The definition of a function can appear in other code snippets, when the function is not executed during the compile phase
It will only be defined if it is executed! Only independent definitions will be compiled in memory!
If it appears in another function body, it needs to be called when the outer function is defined and takes effect!
5. Function names are case insensitive
6. Duplicate names are not allowed, including system functions
7. "Variable function"
Function names can be replaced with other variables
$func _name = "SayHello";
$func _name (); The SayHello () function is called at this time
Note: Variables can only be used when they are called, not allowed when defined!
8. Variables can be called functions as function names, as well as array element values!
9. Form parameter parameter, actual parameter argument
You can pass NULL on the parameter, indicating that the argument does not want to pass the value
The form participation argument can be passed either as a value or as a reference.
Referring to pass parameters, you should include the & symbol in front of the formal parameter when defining the function, and the calling function argument must be a variable
How do I choose which delivery method to use?
A. Need to ensure the integrity of the original data
B. Need for increased efficiency
C. Saving memory by passing large data references
10. Parameter Default value
A. The default value of a function's parameters must be a determined value, not a variable!
As long as the constant is defined before the call, you can use the constant as the default value for the parameter
B. There can be more than one function default value, it is recommended to put parameters with default values in the last side of the parameter list
This makes it possible to call a function without assigning a parameter value with a default value, or an error occurs
C. Default parameters can be non-scalar types, such as arrays, NULL
D. Any default parameter must be placed to the right of any non-default parameter
11. Number of parameters
A. Number of formal parameters more than actual parameters
Report a warning level error and replace it with NULL
B. Arguments more than formal parameters
Do not report an error, in turn, the parameter is assigned a value
C. Indeterminate number of parameters
1) A formal parameter is not defined, and is always an argument more than a formal parameter
2) "Variable quantity parameter"
Func_get_args () Gets the value of all arguments when the current function is invoked, returns an array of all argument values
Func_get_arg () Gets the value of an argument, identified by an index value, E.g:func_get_arg (0)
Func_num_args () Gets the number of all arguments
12. "Return" return value
A. function has only one return value, you can get a similar result by returning an array, but you can have multiple return statements
B. The return statement immediately aborts the function and returns control to the line of code that called the function
C. Can return any type that includes arrays and objects
D. Return of function also score pass and reference pass (returns a variable only)
1) The default is the way the value is passed
2) Reference delivery Method:
-When defining a function, the function name is preceded by A & indicating that the function can return a reference
-When the function is called, the function name is preceded by the & to get the reference returned by the function
At this point, modifying the return value outside the function modifies the value of the return variable within the function.
-If the function needs to return a reference, you need to return a variable to
-Return a reference from the function, you must use the reference operator when the function declares and assigns a return value to a variable &
function &returns_reference () {return $someref;}
$newref =& returns_reference ();
3) Return the role of the reference
/* "Variable scope" */
A. Global variables and local variables
1) No overlap between scopes, i.e. variables of different scopes, inaccessible between
2) global scope-areas outside the function
3) Local scope-the area within the function, each function is a separate scope
B. Super global variables, which can be used both globally and locally, can only be used by the system, and are array variables.
$GLOBALS $_cookie $_env $_files $_get
$_post $_request $_server $_session
C. $GLOBALS
1) cannot have super global variables, but can have hyper-global data!
2) Place the required data in an array of hyper-global variables, but use $globals uniformly
3) $GLOBALS characteristics
-Each global variable is an element within the corresponding $globals!
Whenever a global is added, an element with the same name is automatically added within the $globals!
In the same vein, whenever an element is added, a global variable is added, typically increasing within the function
-Any modifications made will also be mapped to another, including updates and deletions
To access global variables within a function, simply use the $globals
-The global variables that have occurred can be obtained by $globals this array
4) in the PHP life cycle, the so-called global variables that are defined outside the function body are not directly available inside the function
4) Global keyword (not recommended)
Declare a local variable as a ' reference ' to a global variable of the same name! Equivalent to a constant reference pass
Global $var; $var = & $GLOBALS [' var '];
Different from $globals!!!
Global in the function produces an alias variable that points to the external variable of the function, not the actual external variable of the function.
$GLOBALS The actual call is an external variable, and inside and outside the function is always consistent.
The role of global is to define globals, but this global variable is not applied to the entire site, but to the current page, including all files of include or require.
D.
1) scope only for variables, invalid for constants
2) The scope of the variable defined in the loaded file depends on where it is loaded.
function is loaded in the global, function is loaded is local!
/* "Variable life cycle" */
1. When the script ends, the global variable disappears
2. When the function finishes executing, the local variable disappears
3. Static variables
Static keyword
A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope.
The static variable is initialized only once, and the other local variables are re-assigned each time they are called.
The life cycle of static variables declared by the statics is extended.
/* "Iteration and recursion" */
Iterative efficiency is higher than recursion!
Iteration is a thought (algorithm), structured and used like a loop!
Recursion is an idea (algorithm) that splits big problems into small problems and solves small problems to solve big problems.
Solutions that require big problems and small problems are consistent!
Recursive structure and syntactic representation of functions. function body calls the function itself!
Recursive exit: When the problem can be resolved, no more recursion
/* "anonymous function/Closure function" */
An anonymous function, also called a closure function (closures), allows temporary creation of a function without a specified name.
1. When you define an anonymous function, you do not need to increment the function name.
2. The administration of anonymous functions in PHP is handled in an object way.
3. Anonymous functions should be stored in variables.
4. Anonymous functions are implemented by the closure class
5. You can use functions as arguments and return values for functions
6. Declare a function by using the use ($param) to pass in the function to the variable, in combination with the variable reference to implement the closure
7. You can use variables to refer to functions
$func = function ($e) {
Echo $e;
}; Ends with a semicolon ending as if the variable is assigned a value
Var_dump ($func); Using anonymous functions
$func (' Itcast '); Call to function
This is not a mutable function, but an object. Closure closed Bag class
Use syntax
Anonymous functions tend to be the concept of values that may appear anywhere.
Use allows anonymous functions to consume variables of their outer scope. Non-Global!
The difference between use and global:
Use a variable with its outer scope
function out () {
$v = "in Out";
$func = function () use (& $v) {
Var_dump ($v);
}
}
Use the automatic passing of similar parameters, and also supports how values and references are passed.
Role
Often called as ' temporary function ' (a function that is called only somewhere)
For example:
PHP has a array_map () function, which is to call a function for each element within a function
Operation result (Array) = Array_map (Operation function, array of operations);
$result _arr = Array_map (function ($v) {return $v 3}, $arr);
Closure usage Instances
function Closurecreater () {
$x = 1;
return function ($fun = null) use (& $x) {//value by reference
echo "<br/>". $x + +;
$fun and $fun ();
};
}
$x = "Hello World";
$test = Closurecreater ();
$test ();
$test (function () {echo "Closure test One";});
$test (function () {echo "Closure test";});
$test (function () use ($x) {echo "<br/>". $x;});
Saving a function as an array element
$x = ' outer param. ';
$arr = Array ();
$arr [] = function ($STR) use ($x) {return $str. $x;};
echo $arr [0] (' Test fun in arr, ');
/* "Array" */
Associative arrays: keys and values are associated, and keys represent logical meanings of values.
Indexed array: The key and value are not associated, and the key represents the position of the value. Usually subscript starts at 0, increments the element
Count ($var [, $mode])//Count the number of array elements
$mode Optional, recursive statistics when set to 1 or true
$var non-array, returns 1, $var uninitialized or equal to null or an empty array, returns 0
Use of key names
Integer numeric keys do not need to be quoted ($arr [1])
String numeric keys also do not need to be quoted ($arr = Array (' 1 ' = ' abc '); $arr [1])
Associative array, string key must be quoted ($arr = Array (' a ' = = ' aaa '); $arr [' A '])
Associative arrays, which parse variables in double quotes, without quotation marks ($arr = Array (' a ' = = ' aaa '); "$arr [A]")
/* "Pointer" */
Current/pos returns the value of the array cell that is currently pointed to by the internal pointer, and does not move the pointer.
Key returns the key name of the current cell in the array and does not move the pointer
Next moves the inner pointer in the array forward one bit and returns the value of the current cell after the move. Move first, and then take the value.
Prev The inner pointer of the array back to one bit and returns the value of the current cell after the move.
End points The inner pointer of the array to the last cell and returns the value of the last cell
Reset points The inner pointer of the array to the first cell and returns the value of the first array cell
Each returns the current key/value pair in the array and moves the array pointer one step forward.
Returned is a 4-length array of keys and values, subscript 0 and key for keys, subscript 1 and value for values
After each (), the array pointer stays in the next cell in the array
Or, when the end of the array is encountered, stay in the last cell.
If you want to iterate through the array again, you must use Reset ().
1. The above pointer operation function, in addition to key (), if the pointer moves the array, then returns FALSE. A key () is removed to return null.
2. If the pointer is illegal, the next/prev operation can not be performed and the reset/end operation is possible.
3. Current/next/prev returns False if it encounters a containing empty cell (0 or ""). And each will not!
The list assigns the values in the array to some variables. List () is a language structure, not a function
Can only be used for arrays of numeric indexes and assume that the numeric index starts at 0
/* can be used to exchange values for multiple variables */list ($a, $b) = Array ($b, $a);
Example: List ($drink,, $power) = array (' coffee ', ' brown ', ' caffeine ');
1. Copy the array, and the pointer position will be copied.
Exception: If the array pointer is illegal, the copied array pointer is reset, and the pointer to the original array is not changed.
"Pointer Problem"
Whoever writes first will open up a new value space. Regardless of the value passed to the variable (array variable).
The array function current () is defined as a write operation, so there is a problem.
A foreach traversal is a copy of an array that, when written, opens up a new value space.
That is, when the Foreach loop body writes to the original array, a pointer problem occurs.
If the pointer is illegal when a new space is opened, the pointer is initialized.
2. If there is a problem with the pointer position, reset () is initialized to resolve.
/* "Traverse array" */
* Find the element first, then get the key and the value
Foreach
foreach (array_expression as [$key =>] & $value)
When foreach starts executing, the pointer inside the array automatically points to the first cell.
After getting the element information, move the pointer, and then execute the loop body
1. Foreach loop structure, break and continue for foreach
2. Foreach supports alternate syntax for loops.
3. $value is the variable that holds the value of the element, and its modification does not alter the element value of the array
4. $value support Reference copy of element value, add & before $value
5. $key does not support reference delivery
6. Foreach iterates through the copy of the original array, while the operation of the loop body array is the operation of the original
That is, the operation of the loop body array, which takes effect on the original, does not take effect on the traversal.
A copy of an array is used as a traversal
While...list...each
while (list ($key, $val) = Mysql_fetch_row ($result)) = each ($arr) {
echo "$key = $val \ n";
}
PHP most full note (b) (worth collecting, check it through)