Home > Backend Development > PHP Tutorial > PHP control statements_PHP tutorial

PHP control statements_PHP tutorial

WBOY
Release: 2016-07-13 17:21:36
Original
869 people have browsed it

PHP control statement
1. IF statement
The IF statement is an important feature in most languages. It executes program segments based on conditions. PHP's IF statement is similar to C:
if (expr)
statement

As discussed in expressions, expr is evaluated to its truth value. If expr is TRUE, PHP executes the corresponding statement, if it is FALSE, it ignores it.
If $a is greater than $b, the following example will display 'a is bigger than b':
if ($a > $b)
print "a is bigger than b";

Usually, you want to execute more than one statement based on a condition. Of course, there is no need to add an IF judgment to every statement. Instead, multiple statements can be grouped into a statement group.
If statements can be nested within other IF statements, allowing you to flexibly and conditionally execute various parts of the program.
2. ELSE statement
Usually you want to execute one statement when a specific condition is met, and execute another statement when the condition is not met. ELSE is used to do this. ELSE extends the IF statement and executes another statement when the IF statement expression is FALSE. For example, if the following program is executed, if $a is greater than $b, it will display 'a is bigger than b', otherwise it will display 'a is NOT bigger than b':
if ($a>$b) {
print "a is bigger than b”; As the name suggests, it is a combination of IF and ELSE. Similar to ELSE, it extends the IF statement to execute other statements when the IF expression is FALSE. But unlike ELSE, it only executes other statements when the ELSEIF expression is also TRUE.
Multiple ELSEIF statements can be used in one IF statement. The first statement whose ELSEIF expression is TRUE will be executed. In PHP 3, you can also write 'else if' (written as two words) and 'elseif' (written as one word) with the same effect. This is just a small difference in the way it's written (if you're familiar with C, it's the same), the result is exactly the same.
The ELSEIF statement is only executed when the IF expression and any preceding ELSEIF expression are both FALSE, and the current ELSEIF expression is TRUE.
The following is a nested IF statement containing ELSEIF and ELSE:
if ($a==5):
print "a equals 5";
print "...";
elseif ($a==6):
print "a equals 6";
print "!!!";
else:
print "a is neither 5 nor 6";
endif;


4. WHILE statement
The WHILE loop is a simple loop in 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 execute the nested statement repeatedly 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 the nested statement, this execution will not terminate until the end of the loop (each time PHP runs a nested statement is called a loop ). Similar to the IF statement, you can use braces to enclose a group of statements and execute multiple statements in the same WHILE loop:
WHILE(expr): statement ... ENDWHILE;
The following examples are exactly the same, Type the numbers 1 to 10:

/* example 1 */
$i=1;
while ($i<=10) {
print $i++; /* the printed value would be $i before the increment (post- 
 increment) */
 }
 /* example 2 */
 $i=1;
 while ($i<=10) :
print $i; Check if the expression is true at the end of the loop, not at the beginning of the loop.The main difference between it and a strict WHILE loop is that the first loop of DO..WHILE must be executed (the truth expression is only checked at the end of the loop), instead of a strict WHILE loop (checked at the beginning of each loop) Truth expression, if it is FALSE at the beginning, the loop will terminate execution immediately).
 DO..WHILE loop has only one form:
 $i = 0;
 do {
   print $i;
   } while ($i>0);

The above loop is only executed once, because after the first loop, when the truth expression is checked, it calculates to be FALSE ($i Not greater than 0) Loop execution terminates.
6. FOR loop statement
The FOR loop is the most complex loop in PHP. Just like in C. The syntax of a FOR loop is:
FOR (expr1; expr2; expr3) statement
The first expression (expr1) is unconditionally evaluated (executed) at the beginning of the loop.
Each time through the loop, the expression expr2 is evaluated. If the result is TRUE, loops and nested statements continue to execute. If the result is FALSE, the entire loop ends.
At the end of each loop, expr3 is evaluated (executed). Each expression can be empty. If expr2 is empty, the number of loops is variable (PHP defaults to TRUE, like C). Unless you want to terminate the loop with a conditional BREAK statement in place of the FOR truth expression, don't do this.
Consider the following example. They all display numbers 1 to 10:
 /* example 1 */
  for ($i=1; $i<=10; $i++) {
   print $i;
  }
/*Example 2*/
For ($ i = 1;; $ i ++) {
IF ($ i & gt; 10) {
Break;
🎜>
Print $ i;
   }
  /* example 3 */
  $i = 1;
   for (;;) { 🎜> }
     print $i; Empty expression.
Other languages ​​have a foreach statement to iterate over an array or hash table. PHP uses while statements and list(), each() functions to achieve this function.

This news has a total of
2
pages, currently on page
1

1

2


7. SWITCH selection statement
The SWITCH statement is like a series of IF statements for the same expression. Many times, you want to compare the same variable (or expression) with many different values, and execute different program segments based on different comparison results. This is what the SWITCH statement is for.
The following two examples do the same thing in different ways, one using a set of IF statements and the other using a SWITCH statement:
/* example 1 */
if ($i == 0) {
print "i equals 0";
}
if ($i == 1) {
print "i equals 1";
}
if ($i == 2 ) {
  print "i equals 2";
break;
case 1:
print "i equals 1";
break;
case 2:
print "i equals 2";
break; }


(2) REQUIRE statement
The REQUIRE statement replaces itself with the specified file, much like the preprocessing #include in C.
This means that you cannot put the require() statement in a loop structure to include the contents of a different file each time you call the function. To do this, use the INCLUDE statement.
 require(’header.inc’);
(3), INCLUDE statement
The INCLUDE statement includes the specified file.
Every time an INCLUDE is encountered, the INCLUDE statement will include the specified file. So you can use the INCLUDE statement in a loop structure to include a series of different files.
 $files = array('first.inc', 'second.inc', 'third.inc');
 for ($i = 0; $i < count($files); $i++) {
  include($files[$i]); arg_2, ..., $arg_n ) {
 echo "Example function. ";
return $retval



http://www.bkjia.com/PHPjc/532433.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/532433.html
TechArticle

PHP control statement 1. IF statement The IF statement is an important feature in most languages. It executes program segments based on conditions. . PHP's IF statement is similar to C: if(expr) statement As discussed in expressions...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template