Basic php knowledge: Control Structure

Source: Internet
Author: User

The control structure of php is mostly the same as that of other mainstream languages, such as C and Java.

Here are some different and frequently-tested details:

1> alternative Syntax of process control (pascal style)
It is mainly used in if, while, for, foreach and switch statements. The basic form of the alternative syntax is to replace left curly braces ({) with colons (:) and right curly braces (}) with endif;, endwhile;, endfor;, endforeach; and endswitch ;.
Example (1 ):
If ($ a = 5 ):
/* Dosomething1 */
/* Dosomething1 */
Endif;
It is equivalent:
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 statement (frequent test and thorough understanding are also necessary ).
Format ':'...... 'Endfor; 'in the form)
For (expr1; expr2; expr3)
Statement
Running process:
The first expression (expr1) is unconditionally evaluated once before the loop starts.
Expr2 is evaluated before each cycle starts. If the value is TRUE, the loop continues and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
Expr3 is evaluated (executed) after each loop ).
The equivalent while statement is:
Expr1;
While (expr2 ):
Expr3;
Endwhile;

3> the break is different.
Break ends 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 determine the number of loops. Break 1; jumps out of a layer-1 loop.
I don't know if c contains any, because I don't have a c system book.

4> foreach
Format:
A. foreach (array_expression as $ value)
Statement
B. foreach (array_expression as $ key => $ value)
Statement
Note:
Format a traverses the given array_expression array. In each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).
In B format, only the key names of the current Unit are assigned to the variable $ key in each loop.

Note:
A. When foreach starts execution, the pointer inside the array automatically points to the first unit. This means that you do not need to call reset () before the foreach loop (). /* Reset (array & array): Move the internal pointer of array to the first unit of array and return the value */
B. Unless the array is referenced, foreach operates on a copy of the specified array rather than the array itself. Therefore, the array pointer is not changed by the each () structure, and the modification to the returned array unit does not affect the original array. However, the internal pointer of the original array does move forward when processing the array. Assuming that the foreach loop is running till the end, the internal pointer of the original array points to the end of the array.
Since PHP 5, you can easily add & before $ value to modify the array unit. This method will assign values by reference instead of copying a value.
Example:
$ 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 "@" to suppress error messages.

Example of using foreach:
$ Arr = array ("one", "two", "three ");
Reset ($ arr );
While (list (, $ value) = each ($ arr )){
Echo "Value: $ value <br> \ n ";
}
Foreach ($ arr as $ value ){
Echo "Value: $ value <br/> \ n ";
}

5> different continue (I rarely use continue)
Purpose: Use the loop structure to skip the remaining code in this loop and start executing the next loop when the condition value is true.
Like break, it also accepts a number to determine how many layers to jump out to the end of the loop code.
Note: "continue;" is the same as "continue 1". It all jumps to the end of this loop at the current layer. Continue 2 jumps out of the current layer and loops to the end of the outer layer.

6> the function of continue in a switch is similar to break (different from other languages ).

7> declare
Structure is used to set the execution instructions for a piece of code. The declare syntax is similar to other process control structures:
Declare (directive)
Statement
The directive section allows you to set the behavior of the declare code segment. Currently, only one command is known: ticks (for more information, see the following ticks command ).
The statement part of the declare code segment will be executed.

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.