[Transfer to Zhongyuan University PHP video tutorial II

Source: Internet
Author: User
Tags case statement
1. Value assignment

<? Php

$ A = 3;
$ A + = 5; // sets $ a to 8, as if we had said: $ a = $ a + 5;
$ B = "Hello ";
$ B. = "There! "; // Sets $ B to" Hello There! ", Just like $ B = $ B." There! ";

?>

Note the value assignment operationCopy the value of the original variable to the new variable.So changing one does not affect the other. This is also suitable for copying some values such as large arrays in intensive loops. Reference Assignment is supported since PHP 4, and $ var = & $ othervar; Syntax is used, but this is not possible in PHP 3. Referencing a value assignment means that both variables point to the same data without any data copies.

2. What is special about operators than operators? :, The following example is used to solve the problem:

(Expr1 )? (Expr2): (expr3 );

If the calculation result of expr1 is true, the limit row expr2; otherwise, the limit row expr3. In reality, it is a bit similar to if... else loop, but it can make the program more efficient.

3. Process Control

Most of PHP's NLP methods are based on the C statement. Therefore, there is also a circle similar to C statement in terms of process control. Unlike ASP, PHP's dirty circles can use the goto BASIC dirty method,PHP is a structured program statementThe design of the process has a certain degree of planning, instead of using the BASIC concept to skip to another section.

PHP does not have the main () {} section in the C statement. In fact, the entire PHP main Plane (that is, the URL entered by the browser) is main () {} section, which is similar to other programming programs, such as Prel, Python, and Shell Script.

On the segment delimiter of the process, both use {as the start and end of the segment, and use} as the end, which is the same as the C statement.However, C can define begin as the beginning and end as the end (like Pascal), but PHP cannot do this special definition.

WhileIn the PHP coding method, you need to add a score when each command ends. However, you do not need to add a score pair after the ending character.

4. The third type is the if... else loop of nest decision, which is usually used in multiple decision-making decisions. It combines several if. else operations.

Let's look at the example below.

<? Php
If ($ a> $ B ){
Echo "a is larger than B ";
} Elseif ($ a ==$ B ){
Echo "a equals B ";
} Else {
Echo "a is smaller than B ";
}
?>

In fact, please be careful when using this nest if .. else loop becauseToo many programming circles can easily cause problems in the design environment, or if you miss the big margin, it will cause inexplicable problems in the program..

5. while can be included in the upper limit of the upper limit, while endwhile can be added in the upper limit. See the following example.

<? Php
$ I = 1;
While ($ I <= 10 ):
Print $ I ++;
Echo ". Later I dared not <br> \ n ";
Endwhile;
?>

AnotherDo... while repeating the loop first, and then judge whether or not the primary row is required, that is, repeating the loop at least once,It is a bit like a method for playing after the player first. This sort circle is different from while.While is to judge and then handle). If the speaker is familiar with Pascal's statement, he will find the do... while loop is like Pascal's repeat... until loop.

Do .. whilte's explain method is as follows:

Do {
Statement
} While (expr );

6. If the loop has an initial value and all values need to be accumulated (or tired), it is better to use the for loop than to use the while loop. For example, extract the information from the Resource

7. PHP 4 introduces the foreach structure, which is similar to Perl and other languages. This is justA Simple Method for Traversing Arrays. ForeachCan only be used for ArraysAn error occurs when you try to use it for another data type or an uninitialized variable. There are two types of syntax. The second type is secondary, but it is the first type of useful extension.

Foreach (array_expression as $ value)
Statement
Foreach (array_expression as $ key => $ value)
Statement

The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).

In the second format, only the key names of the current Unit are assigned to the variable $ key in each loop.

Foreach"@" Cannot be used to suppress error messages..

8. The switch statement is similar to a series of IF statements with the same expression. In many cases, you need to compare the same variable (or expression) with many different values and execute different codes based on the value it equals. This is exactly the purpose of the switch statement.

Note: unlike other languages, the function of the continue Statement on the switch is similar to break. If there is a switch in the loop and you want to continue to the next cycle in the outer loop, use continue 2.

To avoid errors, it is very important to understand how the switch is executed. The switch statement is executed one by one row (in fact, it is a statement ). No code is executed at the beginning.PHP starts to execute the statement only when the value in a case statement matches the value of the switch expression.Until the switch program segment ends or the first break statement is encountered. If you do not enter break at the end of the case statement segment, PHP will continue to execute the statement segment in the next case.

In the switch statement, the condition is evaluated only once and used to compare with each case statement. In the elseif statement, the condition is evaluated again. If the condition is much more complex than a simple one, or in a loop that occurs many times, the use of the switch statement may be faster.

A case expression can be any simple expression, that is, an integer, a floating point, or a string. Arrays or objects cannot be used unless they are unbound from the simple type.

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.