PHP process control and condition selection

Source: Internet
Author: User
Tags case statement
PHP process control and condition selection are an important part of PHP syntax. if this part cannot be understood thoroughly, we cannot continue learning PHP.

PHP process control and condition selection are an important part of PHP syntax. if this part cannot be understood thoroughly, we cannot continue to learn PHP. Therefore, we must be proficient in using various process condition statements for strategizing! The following comments are important explanations of the syntax. please read them carefully! I need to emphasize that if you use the end method, pay special attention to the semicolon ';' at the end of the statement. due to the limited space, this is only a summative article, in the future, I will give a classic case in the comprehensive instance classification. if there is an error, I am grateful to you for your prompt comment.

Flat view printing?
001 // If can exist independently
002 If (true)
003 {
004 // Statement Block
005 }
006 // Both exist
007 If (true)
008 {
009 // Statement Block
010 }
011 Else
012 {
013 // Statement Block
014 }
015 // Multiple if statements
016 If (true)
017 {
018 // Statement Block
019 }
020 Elseif (true)
021 {
022 // Statement Block
023 }
024 // Multiple if statements
025 If (true)
026 {
027 // Statement Block
028 }
029 Elseif (false)
030 {
031 // Statement Block
032 }
033 Else
034 {
035 // Statement Block
036 }
037 // Use a colon to replace braces {} in the PHP if statement, but the block statement must end with endif.
038 If (true ):
039 // Statement Block
040 Endif;
041 If (true ):
042 // Statement Block
043 Elseif (false): // else if () cannot be written here ():
044 // Statement Block
045 Endif;
046 Switch ($ conditions)
047 {
048 // Execute the case statement block when the expression of the case statement is true.
049 Case $ con:
050 {
051 // Add break at the end of a common statement to terminate the execution of the following case statement
052 }
053 Case $ con2:
054 {
055 // Your statement Block
056 Break;
057 }
058 //...... There can be N case statements.
059
060 // Default process
061 Default:
062 {
063 }
064 }
065 // Alternative writing method
066 Switch ($ cnditions ):
067 // Same as switch
068 Endswitch;
069
070 // Loop statement
071 // For loop statement, which is the most complex loop control statement in php, has three conditional expressions. The syntax is as follows:
072
073 For ($ expr1; $ expr2; $ expr3 ){
074 // Statement
075 }
076 // Expr1 is a required parameter. The first conditional expression is executed at the beginning of the first loop.
077 // Expr2 is a required parameter. The second conditional expression is executed at the beginning of each loop to determine whether the loop continues.
078 // Expr3: Required parameter. The third condition expression is executed at the end of each loop,
079 // Statenebt required parameter. The statement that is executed cyclically after the condition is met
080 // Alternative writing method
081 For ($ expr1; $ expr2; $ expr3 ):
082 // Statement Block
083 Endfor;
084 If (is_array ($ var ))
085 {
086 Foreach ($ varas $ key => $ v)
087 {
088 // Loop traversal $ var usually $ var is an array
089 // If you enter break, the foreach statement is displayed.
090 // If a continue is encountered, it jumps out of the current loop and continues the next loop.
091 }
092 // Alternative writing method
093 Foreach ($ varas $ key => $ v ):
094 // Statement Block
095 Endforeach;
096 }
097 While ($ con)
098 {
099 // Judge first and then execute
100 // Statement Block
101 }
102 // Alternative writing method
103 While ($ con ):
104 // Statement Block
105 Endwhile;
106
107 Do
108 {
109 // Execute the command first and then judge
110 // Statement Block
111 } While ($ con );
112 // The break statement and the continue statement are used to control the exit of the process.
113 /*
114 -- Break jumps out of the entire loop statement
115 -- Continue jumps out of the current loop and continues the next loop
116 */

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.