Home > Article > Backend Development > Sharing key points of PHP process control
This article mainly shares with you the key points of PHP process control, hoping to help everyone.
1. Three ways of traversing arrays in PHP and their differences
1.1. for loop
can only traverse index arrays
1.2. foreach
can traverse index arrays and Associative array
will execute reset(), which is equivalent to pointing the pointer to the beginning of the array
1.3, while, list(),each() combination
such as:
while( list($key,$value) = each(array_expression)){
Loop body
}
Can traverse index arrays and associative arrays
will not execute reset(), It is equivalent to not performing the action of pointing the pointer to the beginning of the array
2. Extended test points: branch structure
2.1. If...elseif
elseif statement can only have one expression that is true , that is, only one statement block can be executed, multiple elseif clauses are in an exclusive relationship
, and conditions with a small scope are always given priority to be processed first
2.2, switch...case...
The difference from if is that the data type of the control expression behind switch can only be integer, floating point, and string.
The function of the continue statement on switch is similar to break
To jump out of the loop outside switch, you can Use continue 2
3. Real question
How to optimize multiple if...elseif statements in PHP?
1. Try to put conditions with a smaller range and greater likelihood before processing
2. The content of the judgment is relatively complex, and the judged value is an integer, floating point type, or string type, then you can use switch ...case...replace
Related recommendations:
php process control function error-prone note sharing
process control in php Detailed explanation of statements and loop control statements
JavaScript Tutorial--Flow Control Statement
The above is the detailed content of Sharing key points of PHP process control. For more information, please follow other related articles on the PHP Chinese website!