How to learn the second day of php in ten days

Source: Internet
Author: User
Objective: to grasp the php process control 1. if... else loop has three structures. The first one is to use the if condition as a simple decision. It means "how to deal with something ". Syntax: if (expr) {statement} where expr is

Objective: to control the php process

1. if... else loop has three structures
The first is to use the if condition as a simple decision. It means 'How to deal with something if something arises '. Syntax:

If (expr) {statement}

Expr is the criterion for determination. it is usually determined by a logical operator number. Statement is part of the qualified fulfillment program. if the program has only one line, braces can be omitted {}.

Example: braces are omitted in this example.

<? Php
If ($ state = 1) echo 'Haha ';
?>

It is important to check whether the equal value is equal to = rather than =. ASP programmers may often make this mistake, and = is a value assignment.

Example: There are three rows in this example, and braces cannot be omitted.

<? Php
If ($ state = 1 ){
Echo 'Haha;
Echo '<br> ';
}
?>


In addition to the if clause, the first two conditions are added with the else clause, which can be used to explain how to handle an event if it is generated '. Syntax:
If (expr) {statement1} else {statement2} example: the above example is corrected to a more complete processing. The else has only one line of executed instructions, so no braces are added.
<? Php
If ($ state = 1 ){
Echo 'Haha ';
Echo '<br> ';
}
Else {
Echo 'Haha ';
Echo '<br> ';
}
?>


The third type is the progressive if... else loop, which is usually used in multiple decision-making decisions. It combines multiple if... else applications for processing.

Let's look at the example below.

<? Php
If ($ a> $ B ){
Echo 'a bigger than B ';
} Elseif ($ a ==$ B ){
Echo 'A is B ';
} Else {
Echo 'a smaller than B ';
}
?>

In the above example, only the second-layer if... else loop is used to compare the two variables a and B. Actually you need to apply this type of delivery if .. when else loops are performed, be cautious with the application. because too many loops easily give the design logic a title, or if you leave less braces, the program will display an inexplicable title.

2. there is only one for loop and there is no change. Its syntax is as follows:

For (expr1; expr2; expr3) {statement}

Expr1 is the initial value of the condition. Expr2 is the condition to be determined. it is usually determined by a logical operator number (logical operators. Expr3 is the part to be executed after the statement is executed. it is used to change the condition for determining the next cycle, such as adding one. Statement is part of the qualified fulfillment program. if the program has only one line, braces can be omitted {}.

The following is an example of using for loop writing.

<? Php
For ($ I = 1; $ I <= 10; $ I ){
Echo 'This is the nth round of '. $ I.' <br> ';
}
?>

3. switch loop, usually processing compound condition determination. each sub-condition is part of the case instruction. In practice, if many similar if commands are applied, they can be combined into a switch loop.

Syntax:

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

The expr condition, usually the variable name. The exprN after case usually represents the variable value. The part that meets the condition after the colon. Be careful to use the break to skip the cycle

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.