Ten days to learn php (1)

Source: Internet
Author: User
Tags switch loop
Ten days to learn php (1) I wrote ten days to learn ASP, and ten days to learn ASP. NET or something. now I want to write another PHP, which is quite complete. I will not talk about the PHP debugging method here. many articles have been introduced and there are many different combinations. I am currently using Apache web server and my SQL as WEB server and database, in the php-4.3.3 environment to do the program. Of course, it is essential to simply build and access the PHPMYADMIN database.

As for form design, I don't want to talk much about it here. I have already introduced it in "Ten days learning ASP.

The following describes the PHP syntax.

1. embedding method:
PHP, similar to ASP, can be Of course, you can also specify it yourself.

2. Reference File:
There are two methods to reference files: require and include.
The use of require is as follows: require ("MyRequireFile. php ");. This function is usually placed at the beginning of the PHP program. before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage. This method can also be used to introduce common functions into webpages.
Include usage methods such as include ("mydomaindefile. php ");. This function is generally placed in the process of process control. The PHP program webpage reads the include file. In this way, you can simplify the process during program execution.

3. annotation method:
Echo "this is the first example. "; // Note the C ++ syntax in this example
/* This example uses multiple rows
Annotation method */
Echo "this is the second example. ";
Echo "is the third example. "; # This example uses UNIX Shell syntax annotations
?>

4. variable type:
$ Mystring = "I am a string ";
$ NewLine = "NewLine ";
$ Int1 = 38;
$ Float1 = 1.732;
$ Float2 = 1.4E + 2;
$ MyArray1 = array ("child", "ugly", "Yin", "Mao ");

Two problems are raised here. First, the PHP variable starts with $, and the second PHP statement ends with;, which may be unsuitable by ASP programmers. These two omissions are also the cause of many program errors.

5. operator number:

Mathematical operation: Symbol meaning + addition operation-subtraction operation * multiplication operation/division operation % remainder operation + + accumulate -- decrease
String operation:
There is only one operator number, which is an English ending. It can be used to connect strings and convert them into new merged strings. Similar &
$ A = "PHP 4 ";
$ B = "powerful ";
Echo $ a. $ B;
?>
Two problems are also raised here. the output statement in PHP is echo, and the second is similar to <% = variable %> in ASP. PHP can also .

Logical operation:
Symbol meaning <less than> greater than <= less than or equal to> = greater than or equal to = equal! = Not equal to & And (and) And (And)
Or (or) Or (Or) xor is different or (Xor )! No (Not) let's talk about it today. let's talk about process control tomorrow.
Objective: to master php process control

1. the if... else loop has three structures. The first one is to use the if condition as a pure judgment. It is interpreted as "how to deal with something ". The syntax is as follows: if (expr) {statement} where expr is the condition for judgment. it is usually determined by a logical operator number. Statement is the execution part of the program that meets the conditions. if the program has only one line, you can omit braces {}. Example: braces are omitted in this example. If ($ state = 1) echo "haha ";
?>

It is important to note that, to determine whether the equality is equal to = rather than =, ASP programmers may often make this mistake, = is a value assignment. Example: the execution part of this example has three rows and the braces cannot be omitted. If ($ state = 1 ){
Echo "haha;
Echo"
";
}
?> In addition to the if clause, the first two conditions of else can be interpreted as "what to do if something happens, otherwise how to solve it ". The syntax is as follows: if (expr) {statement1} else {statement2} example: the above example is modified to a more complete processing. Because else only executes one line of commands, no braces are added. If ($ state = 1 ){
Echo "haha ";
Echo"
";
}
Else {
Echo "haha ";
Echo"
";
}
?> The third type is the recursive if... else loop, which is usually used for multiple decision making decisions. It combines several if... else values for processing. Let's look at the example below. If ($ a> $ B ){
Echo "a is larger than B ";
} Elseif ($ a ==$ B ){
Echo "a equals B ";
} Else {
Echo "a is smaller than B ";
}
?> In the above example, we only use the layer-2 if .. else loop to compare the two variables a and B. Actually, we need to use this recursion if .. please be careful when using else loops, because too many layers of loops may cause problems in the design logic, or if you leave less braces, the program may encounter inexplicable problems.

2. the for loop has only one type and does not change. Its syntax is as follows: for (expr1; expr2; expr3) {statement} where expr1 is the initial value of the condition. Expr2 is the condition for judgment. it is usually determined by a logical operator number (logical operators. Expr3 is the part to be executed after statement is executed. it is used to change the condition for the next loop judgment, such as adding one. Statement is the execution part of the program that meets the conditions. if the program has only one line, you can omit braces {}. The following is an example of using for loop writing. For ($ I = 1; $ I <= 10; $ I ++ ){
Echo "this is the first loop". $ I ."
";
}
?>

3. switch loop, usually processing compound condition judgment. each sub-condition is part of the case instruction. In practice, if many similar if commands are used, they can be combined into a switch loop. 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 part that meets the condition after the colon. Note that you must use the break to skip the cycle. Switch (date ("D ")){
Case "Mon ":
Echo "today Monday ";
Break;
Case "Tue ":
Echo "Tuesday ";
Break;
Case "Wed ":
Echo "Wednesday ";
Break;
Case "Thu ":
Echo "Thursday ";
Break;
Case "Fri ":
Echo "Today's Friday ";
Break;
Default:
Echo "today's holiday ";
Break;
}
?>

Here, you need to note break; do not miss it, default. it is acceptable to omit it.
Obviously, it is very troublesome to use the if loop in the above example. Of course, in the design, the conditions with the highest probability should be placed at the beginning, and the minimum conditions should be placed at the end, which can increase the execution efficiency of the program. In the previous example, because the probability of occurrence is the same every day, you do not need to pay attention to the order of conditions. Let's talk about database usage today.

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.