10 days Learn PHP Day 2

web front end five thousand nine hundred and twenty-four 14 years ago (2011-04-08)

Learning purpose: master the process control of php

1. If.. Else cycle has three structures

The first is to use the if condition as a simple judgment. Explain as "what to do if something happens". The syntax is as follows:

    if (expr) { statement }

The expr is the condition of judgment, and usually the logic operation symbol is used as the condition of judgment. While statement is the qualified execution part of the program. If the program has only one line, the curly braces {} can be omitted.

Example: The braces are omitted in this example.

PHP code
  1. <? php    
  2.    if  ( $state ==1) echo   Ha ha  ;     
  3. ?>   

Special attention is paid here to the fact that the judgment of equality is==rather than=, ASP Programmers may often make this mistake,=is assignment.

Example: The executive part of this example has three lines, and braces cannot be omitted.

PHP code
  1. <? php    
  2.    if  ( $state ==1) {   
  3.    echo   "Ha ha;  
  4.   echo " <br>";    
  5.   }    
  6. ?>;   

The second is to add else conditions in addition to if, which can be explained as "how to deal with something if it happens, or how to solve it otherwise". The syntax is as follows

If (expr) {statement1} else {statement2} Example: The above example is modified to more complete processing. In the else, there is only one line of instructions to be executed, so there is no need to add curly braces.

PHP code
  1. <? php    
  2.    if  ( $state ==1) {    
  3.    echo   Ha ha  ;     
  4.    echo   "<br>" ;    
  5.   }    
  6.    else {   
  7.    echo   "Hehe" ;   
  8.    echo   "<br>" ;     
  9.   }    
  10. ?>   

The third is the recursive if.. Else loop, which is usually used for multiple decision making. It combines several if.. Else.

Look directly at the following examples

PHP code
  1. <? php    
  2.    if  (  $a  >  $b  ) {    
  3.    echo   "A is larger than b"  ;     
  4.   }  elseif  (  $a  ==  $b  ) {    
  5.    echo   "A equals b"  ;     
  6.   }  else  {    
  7.    echo   "A is smaller than b"  ;     
  8.   }    
  9. ?>   

In the above example, only two-layer if.. Else loops are used to compare variables a and b. When you actually want to use this recursive if.. Else loop, please use it carefully, because too many layers of loops are likely to cause problems in the logic of the design, or less curly braces will cause inexplicable problems in the program.

2. There is only one for loop, and its syntax is as follows

    for (expr1; expr2; expr3) { statement }

Where expr1 is the initial value of the condition. Expr2 is the judgment condition, which is usually the logical operators. Expr3 is the part to be executed after the statement is executed. It is used to change conditions for the next cycle judgment, such as adding one wait. While statement is the qualified execution part of the program. If the program has only one line, the curly braces {} can be omitted.

The following example is written with the for loop.

PHP code
  1. <? php    
  2.    for  (  $i  = 1 ;   $i  <= 10 ;   $i  ++) {    
  3.    echo   "This is the No." . $i . "Sub cycle<br>"  ;     
  4.   }    
  5. ?>   

3. The switch loop usually handles compound conditional judgment. Each sub condition is a case instruction part. In implementation, if you use many similar if instructions, you can synthesize them into switch loops.

The syntax is as follows

    switch (expr) { case expr1: statement1; break; case expr2: statement2; break; default: statementN; break; }

The expr condition is usually the variable name. The exprN after case usually represents the variable value. The colon is followed by the part to be executed if the condition is met. Note that break should be used to jump out of the loop.

PHP code
  1. <? php    
  2.    switch  (  date  (  "D"  )) {    
  3.    case   "Mon"  :    
  4.    echo   Today Monday  ;     
  5.    break ;     
  6.    case   "Tue"  :    
  7.    echo   Today Tuesday  ;     
  8.    break ;     
  9.    case   "Wed"  :    
  10.    echo   Today Wednesday  ;     
  11.    break ;     
  12.    case   "Thu"  :    
  13.    echo   Today Thursday  ;     
  14.    break ;     
  15.    case   "Fri"  :    
  16.    echo   "Today is Friday"  ;     
  17.    break ;     
  18.    default :    
  19.    echo   "Today's holiday"  ;     
  20.    break ;     
  21.   }    
  22. ?>   

What should be noted here is break; Don't omit, default. It is OK to omit.

Obviously, it is troublesome to use the if loop in the above example. Of course, in the design, the conditions with the highest probability of occurrence should be placed first, and the conditions with the least probability of occurrence should be placed last, which can increase the execution efficiency of the program. The above example does not need to pay attention to the order of conditions because the probability of occurrence is the same every day.

That's all for today and tomorrow data base Use of.