PHP's control structure, most of the same as other mainstream languages, such as C,java.
Here are some of the different and often tested details:
1> alternative syntax for Process Control (Pascal style)
Mainly used in If,while,for,foreach and switch statements. The basic form of substitution syntax is to replace the left curly brace ({) with a colon (:) and the right curly brace (}) into Endif;,endwhile;,endfor;,endforeach; and Endswitch;.
Example (1):
if ($a = = 5):
/*dosomething1*/
/*dosomething1*/
endif
is equivalent to:
if ($a = = 5) {
/*dosomething1*/
/*dosomething1*/
}
Example (2):
if ($a = = 5):
echo "a equals 5";
echo "...";
ElseIf ($a = = 6):
echo "a equals 6";
echo "!!!";
Else
echo "A is neither 5 nor 6";
endif
2>for statements (often test, and thorough understanding is also necessary).
Format: (Support ': ' ... ' endfor; ' instead of {} in the form of}
for (EXPR1; expr2; expr3)
Statement
Run the process:
The first expression (EXPR1) is evaluated unconditionally once before the loop begins.
The EXPR2 is evaluated before each cycle begins. If the value is TRUE, the loop continues and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
The EXPR3 is evaluated (executed) after each loop.
The equivalent while statement is:
EXPR1;
while (EXPR2):
EXPR3;
Endwhile;
3>break is different.
The effect of break is to end the execution of the current for,foreach,while,do-while or switch structure.
At the same time the break can be followed by a number to decide to jump out of several layers of loops. Break 1; To jump out of a 1-layer loop.
I don't know if C is there, because I don't have a C-language system book.
4>foreach
Format:
A.foreach (array_expression as $value)
Statement
B.foreach (array_expression as $key = $value)
Statement
Description
A format iterates over the given array of array_expression. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).
b format does the same thing, except that the key name of the current cell is assigned to the variable $key in each loop.
Attention:
A. When foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the Foreach loop. /*reset (array &array): Moves the internal pointer of the array to the first cell of the arrays and returns the value */
B. Unless the array is referenced, foreach operates on a copy of the specified array, not the array itself. So the array pointers are not changed by the each () structure, and the changes to the returned array cells do not affect the original array. However, the internal pointer of the original array does move forward in the process of handling the array. Assuming that the Foreach loop runs to the end, the internal pointer to the original array will point to the end of the array.
Starting with PHP 5, it is easy to modify the cells of an array by adding & to it before $value. This method assigns a value to a reference instead of copying a value.
Cases:
$arr = Array (1, 2, 3, 4);
foreach ($arr as & $value) {
$value = $value * 2;
}
$arr is now Array (2, 4, 6, 8)
C.foreach does not support the ability to suppress error messages with "@".
Using the foreach Example:
$arr = Array ("One", "one", "three");
Reset ($arr);
while (list (, $value) = each ($arr)) {
echo "Value: $value
\ n ";
}
foreach ($arr as $value) {
echo "Value: $value
\ n ";
}
5>continue different (I seldom use continue)
Function: The loop structure is used to skip the remaining code in this loop and start the next loop when the condition evaluates to True.
As with break, a number is accepted to decide to jump out of several layers to the tail of the Loop code.
Note: continue; and continue 1; like, all jumps to the end of this cycle of this layer. Continue 2 jumps out of this layer and loops to the end of the outer.
The role of continue in 6>switch: similar to break (unlike other languages).
7>declare
Structure is used to set execution instructions for a piece of code. The syntax of declare is similar to other process Control structures:
DECLARE (Directive)
Statement
The directive section allows you to set the behavior of the declare code snippet. Currently only one command is known: ticks (see ticks instructions below for more information).
The statement section in the declare code snippet will be executed and what side effects in execution are dependent on the instructions set in directive.
The declare structure can also be used for global scope, affecting all subsequent code.
The main example is for tricks (currently only tricks):
For example:
function profile ($dump = FALSE)
{
Static $profile;
Return The Times stored in profiles, then erase it
if ($dump) {
$temp = $profile;
Unset ($profile);
return ($temp);
}
$profile [] = Microtime ();
}
Register function profile is ticks function
Register_tick_function ("Profile");
Initialization
Profile ();
Run a piece of code and invoke a function profile () when executing a 2-sentence (ticks=2) simple statement;
Declare (ticks=2) {
for ($x = 1; $x < + + $x) {
Echo Similar_text (MD5 ($x), MD5 ($x * $x)), "
;";
}
}
Display data stored in profile stores (profiles)
Print_r (TRUE);
Attention:
Register_tick_function () should not being used with threaded webserver modules. Ticks is not working in ZTS mode and may crash your webserver.
Can not be used in multi-channel processing module (?? Don't understand?? What is a multi-channel processing module?) Server, or it will crash. I have crash many times. Depressed.
8>require and include
Different points:
The include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing a page when you encounter a lost file, use require (). The include () is not the case and the script will continue to run. Also be sure to set the appropriate include_path. Note Before PHP 4.3.5, a syntax error in the include file does not cause the program to stop, but from this version.
The same point and usage:
A. Variable range:
When a file is contained, it contains code that inherits the range of variables for the row containing the statement. From there, any variables that are available to the calling file at that line are also available in the called file. However, all functions and classes defined in the include file have global scope.
If the "include statement" appears in a function in the calling file, all code contained in the called file will behave as if they were defined inside the function. So it will follow the variable range of the function.
B. Parsing mode
When a file is included, the parser goes out of PHP mode at the beginning of the target file and enters HTML mode to resume at the end of the file. For this reason, any code in the target file that should be executed as PHP code must be included in the valid PHP start and end tags.
C. Formatting problems in conditional statements
Because include () and require () are special language constructs, they must be placed in a statement group (in curly braces) in a conditional statement.
Because include () is a special language structure, its parameters do not require parentheses. Be careful when comparing the return value.
D. Handling return values
You can use the return () statement in the included file to terminate the execution of the program in the file and return the script that called it. It is also possible to return a value from a contained file. The return value of the include call can be obtained just like a normal function. However, this does not work with remote files unless the remote file's output has a valid PHP start and end tag (as with any local file). You can define the desired variable within the tag, which is available after the file is contained.
Example:
return.php
==============
$var = ' PHP ';
return $var;
noreturn.php
==============
$var = ' PHP ';
testreturns.php
=============================
$foo = include ' return.php ';
Echo $foo; Prints ' PHP '
$bar = include ' noreturn.php ';
Echo $bar; Prints 1
E. Problems with the redefinition of functions and variables.
To prevent this from occurring, you can use Include_once or require_once
F. Other:
The following rules apply before PHP 4.0.2: require () always attempts to read the target file, even if the row it is in is not executed at all. Conditional statements do not affect require (). However, if the row where require () is not executed, the code in the destination file will not execute. Similarly, the cyclic structure does not affect the behavior of require (). Although the code contained in the destination file is still the body of the loop, require () itself runs only once
http://www.bkjia.com/PHPjc/317419.html www.bkjia.com true http://www.bkjia.com/PHPjc/317419.html techarticle PHP's control structure, most of the same as other mainstream languages, such as C,java. Here are some of the different and often tested details: 1 alternative syntax for Process Control (Pascal style) master ...