PHP's control structure is mostly 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 alternative syntax is to replace the left curly brace ({) with a colon (:), and the right curly brace (}) into Endif;,endwhile;,endfor;,endforeach respectively; 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 tested, and thoroughly understood are also necessary).
Format: (Support ': ' ... ' endfor ') (in lieu of {})
for (EXPR1; expr2; expr3)
Statement
Run the process:
The first expression (EXPR1) is evaluated unconditionally before the start of the loop.
EXPR2 is evaluated before each loop starts. If the value is TRUE, the loop continues and the nested loop statement executes. If the value is FALSE, the loop is terminated.
The EXPR3 is evaluated after each loop (execution).
The equivalent while statement is:
EXPR1;
while (EXPR2):
EXPR3;
Endwhile;
3>break the difference.
The function of a 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 the 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 traverses the given array_expression array. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array moves forward one step (so the next cell will be taken in the next loop).
The 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 the foreach starts executing, the pointer inside the array will automatically point 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 an array to the first cell of the array, and returns the value * *
B. Unless the array is referenced, foreach operates a copy of the specified array, rather than the array itself. Therefore, the array pointer is not changed by each () structure, nor does the modification of the returned array cells affect the original array. But the internal pointers to the original array did move forward in the process of processing the array. Assuming that the Foreach loop runs to the end, the internal pointer to the original array points to the end of the array.
Since PHP 5, you can easily modify the array's cells by adding & 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 use "@" to suppress error messages.
Use the foreach example:
$arr = Array ("One", "two", "three");
Reset ($arr);
while (the list (, $value) = each ($arr)) {
echo "Value: $value <br>\n";
}
foreach ($arr as $value) {
echo "Value: $value <br/>\n";
}
The difference of 5>continue (I rarely use continue)
function: In a loop structure, use to skip the remaining code in this loop and start the next loop when the condition is evaluated as true.
As with break, a number is also accepted to decide to jump out of several layers to the end of the loop code.
Note: Continue and continue 1; same, jump to the end of this cycle of this layer. Continue 2 jumps out of the loop to the end of the outer layer.
The role of continue in 6>switch: similar to break (unlike other languages).
7>declare
Structure is used to set the execution instructions for a section of code. Declare syntax is similar to other process Control structures:
DECLARE (Directive)
Statement
The directive section allows you to set the behavior of declare code snippets. Only one instruction is known: ticks (see ticks instructions below for more information).
The statement portion of the declare code snippet will be executed and what side effects in the execution occur depending 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 to the Times stored in profile, then erase it
if ($dump) {
$temp = $profile;
Unset ($profile);
return ($temp);
}
$profile [] = Microtime ();
}
Registration function profile is ticks function
Register_tick_function ("Profile");
Class
Profile ();
Run a piece of code, and when executing a 2-sentence (ticks=2) Simple statement, call the function profile ();
Declare (ticks=2) {
for ($x = 1; $x < + + $x) {
Echo Similar_text (MD5 ($x), MD5 ($x * $x)), "<br/>;";
}
}
Show data stored in the Overview store (profile)
Print_r (TRUE);
Attention:
Register_tick_function () should is used with threaded webserver modules. Ticks are not working in ZTS mode and may crash your.
cannot be used in multi-channel processing module (?? Don't understand?? What is a multi-channel processing module? Server, otherwise it will crash. I've been crash a few times. Depressed.
8>require and include
Different points:
Include () produces a warning and require () causes a fatal error. In other words, if you want to stop processing the page when you encounter a missing file, use require (). Include () is not the case, the script will continue to run. Also, be sure to set the appropriate include_path. Note that the syntax error in the include file does not cause the program to stop until PHP 4.3.5, but after this version.
Same point and usage:
A. Variable scope:
When a file is contained, the code it contains inherits the range of variables in the row containing the statement. Starting at this point, any variables that the calling file is available at that line are also available in the called file. However, all functions and classes defined in the include file have a global scope.
If the include statement appears in a function in the calling file, all the code contained in the called file will behave as if they were defined inside the function. So it will follow the variable scope of the function.
B. Analytical mode
When a file is included, the parser moves out of the PHP mode at the beginning of the target file and into HTML mode to recover at the end of the file. For this reason, any code in the target file that should be executed as a PHP code must be included in a valid PHP start and end tag.
C. Formatting problems in conditional statements
Because include () and require () are special language constructs, you must put them in a statement group (in curly braces) in a conditional statement.
Because include () is a special language structure, its arguments do not require parentheses. Be aware when comparing its return value.
D. Processing return values
You can use the return () statement in the included file to terminate the execution of the program in the file and back the script that called it. You can also return a value from a contained file. You can get the return value of the include call like a normal function. However, this does not work when the remote file is included, unless the output of the remote file has a valid PHP start and end tag (like any local file). You can define the desired variable within the tag, which is available after the location where 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. Questions about function and variable redefinition.
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 tries to read the target file, even if its row is not executed at all. Conditional statements do not affect require (). However, if the row of require () is not executing, the code in the destination file is not executed. 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, the require () itself will only run once
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