In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the detailed introduction of different control statements of PHP". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "the detailed introduction of PHP different control statements"!
PHP control statement 1, IF statement
IF statement is an important feature of most languages, which executes program segments according to conditions. The IF statement of PHP is similar to C:
If (expr)
Statement
As discussed in the expression, expr is evaluated as its true value. If expr is TRUE, PHP executes the statement, and if FALSE, it is ignored.
If $an is greater than $b, the following example shows'an is bigger than b':
If ($a > $b)
Print "an is bigger than b"
Usually, you want to execute more than one statement based on the condition. Of course, you don't need to add IF judgment to every statement. Instead, multiple statements can be grouped into a statement group.
If statements can be nested in other IF statements, giving you the flexibility to execute various parts of the program conditionally.
PHP control statement 2, ELSE statement
Usually you want to execute one statement when a specific condition is met, and another statement is executed if the condition is not met. That's what ELSE is for. ELSE extends the IF statement to execute another statement when the IF statement expression is FALSE. For example, the following program executes displaying'an is bigger than b', otherwise display'an is if $an is greater than $b
NOT bigger than b': if ($a > $b) {print "an is bigger than b";} else {print "an is NOT bigger than b";}
PHP control statement 3, ELSEIF statement
ELSEIF, as the name suggests, is a combination of IF and ELSE, similar to ELSE, which extends IF statements to execute other statements when the IF expression is FALSE. But unlike ELSE, it executes other statements only when the ELSEIF expression is also TRUE.
You can use multiple IF statements in one ELSEIF statement. * statements whose ELSEIF expression is TRUE will be executed. In PHP 3, you can also write the same effect as' elseif' (write in two words) and 'elseif' (write in one word). This is just a slight difference in writing (if you are familiar with C, so is it), and the result is exactly the same.
The ELSEIF statement is executed only when the IF expression and any previous ELSEIF expressions are FALSE and the current ELSEIF expression is TRUE.
Here is an IF statement with a nested format of ELSEIF and ELSE:
If ($adept 5): print "an equals 5"; print "..."; elseif ($aquifer 6): print "an equals 6"; print "!!"; else: print "an is neither 5 nor 6"; endif
PHP control statement 4, WHILE statement
The WHILE loop is a simple loop of PHP 3. Just like in C. The basic format of the WHILE statement is:
WHILE (expr) statement
The meaning of the WHILE statement is very simple. It tells PHP to repeat nested statements as long as the WHILE expression is TRUE. The value of the WHILE expression is checked at the beginning of each loop, so even if its value is changed within a nested statement, this execution does not stop until the end of the loop (each time PHP runs a nested statement is called a loop). Similar to IF statements, you can enclose a set of statements in curly braces and execute multiple statements in the same WHILE loop:
WHILE (expr): statement... ENDWHILE
The following examples are exactly the same, with the numbers 1 to 10:
/ * example 1 * /
$item1; while ($i0)
The above loop is executed only once, because after * loops, when the truth expression is checked, it calculates that the FALSE ($I is not greater than 0) loop execution terminates.
PHP control statement 5. FOR loop statement
The FOR loop is the most complex loop in PHP. Just like in C. The syntax of the FOR loop is:
FOR (expr1; expr2; expr3) statement
* expressions (expr1) are evaluated (executed) unconditionally at the beginning of the loop.
With each loop, the expression expr2 is evaluated. If the result is TRUE, the loop and nested statements continue to execute. If the result is FALSE, the entire loop ends.
At the end of each loop, the expr3 is calculated (executed). Each expression can be empty. If expr2 is empty, the number of cycles varies (PHP defaults to TRUE, like C). Don't do this unless you want to end the loop with a conditional BREAK statement instead of the truth expression of FOR.
Consider the following example. They all show the numbers 1 to 10:
/ * example 1 * /
For ($for 1; $i10) {break;} print $I;} / * example 3 * / $ipolar 1; for (;;) {if ($I > 10) {break;} print $I; $iSuppli;}
Of course, the * * examples are obviously *, but you can find that empty expressions can be used in many cases in the FOR loop.
Other languages have a foreach statement to traverse an array or hash table. PHP uses the whilestatement and the list () and each () functions to achieve this function.
PHP control statement 6. SWITCH select statement
The SWITCH statement is like a series of IF statements on the same expression. In many cases, you want to compare the same variable (or expression) with many different values, and execute different program segments according to different comparison results. This is what the SWITCH statement is for.
The following two examples do the same thing in different ways, one with a set of IF statements and the other with SWITCH statements:
/ * example 1 * /
If ($I = = 0) {print "I equals 0";} if ($I = = 1) {print "I equals 1";} if ($I = = 2) {print "I equals 2";} / * example 2 * / switch ($I) {case 0: print "I equals 0"; break; case 1: print "I equals 1"; break; case 2: print "I equals 2"; break } at this point, I believe you have a deeper understanding of "detailed introduction of different control statements in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.